ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.44k stars 31 forks source link

allOf doesn't work #86

Closed winston0410 closed 2 years ago

winston0410 commented 2 years ago

Not quite sure how to reproduce this in a useful way, I have a schema like this, which extends a schema in allOf. Even though I have ensure that the accept exists in header in programmatic level, it is not reflected in the type generated by this library.

import { FromSchema } from "json-schema-to-ts";

const defHeaderSchema = {
    type: "object",
    properties: {
        header: {
            properties: {
                accept: {
                    type: "string",
                    pattern: "application/(?:\\*|json)|\\*/\\*"
                }
            },
            required: ["accept"]
        }
    },
} as const

const postSchema = {
    allOf: [ defHeaderSchema ],
    type: "object",
    properties: {
        body: {
            type: "object",
            properties: {
                number: {
                    type: "string"
                },
            },
            required: ["number"]
        }
    }
} as const;

let obj!: FromSchema<typeof postSchema>

See the final type by hovering on obj

ThomasAribart commented 2 years ago

Hi @winston0410 ! See https://github.com/ThomasAribart/json-schema-to-ts/issues/79 : Your header schema misses a type: "object" keyword !

winston0410 commented 2 years ago

Right after adding that back in, the type is generated perfectly. Thanks!