GUIDE深度 · Deep dive

JSON to TypeScript: interface or type

Objects usually use interface; unions/aliases/arrays use type; they are interchangeable in many cases. The hard part is expressing optional fields, null, array element types and nested objects, plus runtime validation.

更新 2026-10-31 · 公式可核验

Choosing

Objects/extensible: interface.

Unions/mapped/aliases: type.

Array elements: `Item[]`.

Do not miss

Missing keys → `?`.

Possible null → `| null`.

Empty arrays → define the element type first.

FAQ

Can I use the generated types directly?

As a starting point; add unions/validation/docs for production.

Why runtime validation?

TS types disappear at runtime; external JSON may still mismatch.

Sources

Related calculators

JSON to TypeScriptPaste JSON to generate interfaces/types; array objects merge fields and missing keys become optional, all locally.

More guides

API JSON Validation: Types, Optional Fields and VersionsRead guide