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

allOf is generated as an union type

Open ericmorand opened this issue 3 years ago • 0 comments

We are facing a very strange behavior with allOf: the generated type is a type alias instead of a proper type.

Take this code sample:

import {compile, JSONSchema} from "json-schema-to-typescript";

const schema: JSONSchema = {
    allOf: [
        {
            type: "object",
            properties: {
                foo: {
                    type: "string"
                }
            }
        },
        {
            type: "object",
            properties: {
                bar: {
                    type: "string"
                }
            }
        }
    ]
};

compile(schema, 'Schema').then((output) => {
    console.log(output);
})

The content of output is:

export type Schema = {
        foo?: string;
        [k: string]: unknown;
    } & {
        bar?: string;
        [k: string]: unknown;
    }

Instead of the expected:

export type Schema = {
        foo?: string;
        bar?: string;
        [k: string]: unknown;
    }

It creates a very big quality issue: every schema referenced by a allOf directive is exported as a discrete type, making them publicly available and thus contractual. They are internal schemas, just known at schema-writing time and they should not be emitted as some public types.

Is this a known issue?

ericmorand avatar Oct 13 '22 12:10 ericmorand