ThomasAribart / json-schema-to-ts

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

[bug] anyOf/union types not working with nulls #42

Closed domdomegg closed 3 years ago

domdomegg commented 3 years ago

Summary

anyOf and arrays of types in JSON schemas does not appear to behave correctly with nulls. Instead of unioning the type with null, nothing seems to happen.

Details

json-schema-to-ts version: 1.6.4 typescript version: 4.4.3

Example

import { FromSchema } from 'json-schema-to-ts';
const mySchema = { anyOf: [{ type: "string" }, { type: "null" }] } as const
type MyType = FromSchema<typeof mySchema>
import { FromSchema } from 'json-schema-to-ts';
const mySchema = { type: ["string", "null"] } as const
type MyType = FromSchema<typeof mySchema>

In both these examples, MyType should be string | null, but is instead string

ThomasAribart commented 3 years ago

Hello @domdomegg !

It seems working fine on my part. Can you check you have activated the strictNullCheck or strict option in your tsconfig.json file ?

domdomegg commented 3 years ago

You are correct - enabling strictNullChecks in my tsconfig fixes the problem! Apologies for the incorrect report and thanks for such a great library :raised_hands:.