json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Add union-type for arrays with "uniqueItems"

Open JustinLex opened this issue 6 years ago • 1 comments

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>
}

JustinLex avatar Jul 09 '19 10:07 JustinLex

This could be a good approach, and I'd be open to a flag for it if there's more interest from others.

bcherny avatar Nov 29 '20 23:11 bcherny