ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.43k stars 30 forks source link

tuple schema does not work when used in an object #113

Closed winston0410 closed 1 year ago

winston0410 commented 1 year ago

Based on the readme, this is how we get a tuple type from this library:

const tupleSchema = {
    type: "array",
    items: [{ type: "boolean" }, { type: "string" }],
    additionalItems: false,
} as const;

type Tuple = FromSchema<typeof tupleSchema>;

Which will give:

[] | [boolean] | [boolean, string] | [boolean, string, ...unknown[]]

But when I tried to put it in an object like this:

const tupleInObjSchema = {
    type: "object",
    properties: {
        foo: {
            type: "array",
            items: [{ type: "boolean" }, { type: "string" }],
            additionalItems: false,
        }
    },
    additionalProperties: false,
    required: ['foo']
} as const

type TupleInObj = FromSchema<typeof tupleInObjSchema>;

I get the following type:

{
  foo: unknown[]
}

Is it a bug or something intended?

orekav commented 1 year ago

I am facing the exact same issue

ThomasAribart commented 1 year ago

Hi @orekav and @winston0410

Can you try with v2.7.2 ? It seems like it's fixed

winston0410 commented 1 year ago

I can confirm that the fix worked for me. Thank you so much for fixing this issue.