json-schema-to-typescript
json-schema-to-typescript copied to clipboard
Add union-type for arrays with "uniqueItems"
Hi, I'm using this tool in my project and I was thinking that it would be good if the tool added a union type for arrays when translating a JSON schema if the array has uniqueItems set to true. The Set() object is an ordered list that only contains unique elements, so it's perfect for working with JSON using this schema in Typescript. The only issue is that JSON arrays aren't parsed as Set().
I think having a union type for these arrays would be useful to allow programmers to convert these arrays to Set() in place when parsing the JSON.
A property in the JSON schema like this:
{
"itemSet": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true
}
}
Would be represented in the interface generated like this:
{
itemSet: string[] | Set<string>
}
This could be a good approach, and I'd be open to a flag for it if there's more interest from others.