WORKED EXAMPLE · SYNTHETIC INPUT
Infer optional fields without erasing null from JSON samples
Two records have different shapes. Declaring every observed property as required produces a type that rejects the second record.
阅读中文版
These inputs demonstrate reproducible behaviour; they are not customer incidents or evidence of real usage.
Reproducible input
[{"id":1,"note":null,"tags":["admin"]},{"id":2,"tags":[]}]Steps
- Generate TypeScript from both records in one array.
- Inspect note: it is optional because the second record omits it; its observed type is null.
- Add "note":"checked" to the second record and generate again. The field is now required with a null | string union.
Expected result
The original sample generates an optional note and Array<string> tags. The empty tags array does not introduce an any type.
Limits and manual checks
Samples cannot establish all valid future values. Dates stay strings, empty arrays alone use unknown, and optionality reflects only the supplied records. Review the generated type against the actual API contract.
Technical reference