ThomasAribart / json-schema-to-ts

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

schema object without a type attribute #79

Closed NikitaIT closed 2 years ago

NikitaIT commented 2 years ago

Use case: does-json-schema-to-ts-work-on-json-file-schemas

Look at: schema-object-without-a-type-attribute-in-swagger-2-0

const Account = {
    properties: {
        balance: {
            type: 'string',
            description: 'balance in unit WEI, presented with hex string',
            example: '0x47ff1f90327aa0f8e',
        },
        energy: {
            type: 'string',
            description: 'energy in uint WEI, presented with hex string',
            example: '0xcf624158d591398',
        },
        hasCode: {
            type: 'boolean',
            description: 'whether the account has code',
            example: false,
        },
    },
} as const;
type Dog = FromSchema<typeof Account>;

// Now: type Dog = unknown
// Expected: type Dog = { balance, ... }
mauricioklein commented 2 years ago

You forgot type: "object" before properties

ThomasAribart commented 2 years ago

@NikitaIT sorry I didn't respond earlier. I'm not so sure to want to resolve this issue.

From the spec (and the comment in your linked stack overflow page), type Dog should be a union: { balance, ... } | string | null | boolean | number | unknown[]. Which is not much better than unknown, and would increase type computations and reduce performances.