JSON to TypeScript Interface Converter
Turn an API response into editable TypeScript interfaces. Paste several records to expose missing fields and mixed types; conversion stays in your browser. Generated types describe your samples, not every response your API could return.
typetools
instant, client-side type converter • 0ms network latency
export interface Invoice {
id: string;
amount: string;
note?: null;
}
export interface RootPayload {
invoices: Invoice[];
tags: (string | number | null)[];
empty: unknown[];
}architecture & type system comparison
specification breakdown and memory characteristics
specification foundations & design rationale
JSON samples describe observed values rather than a complete API contract. This converter turns those observations into TypeScript interfaces and type aliases, with separate declarations for nested objects. Use several representative records and review the output before sharing it with consumers.
memory layout & runtime overhead
TypeScript interfaces exist purely in memory during the compilation phase within tsserver and tsc. Once compilation completes, the TypeScript AST is transformed by erasing all interface declarations, type aliases, and type annotations from the emitted JavaScript output. Consequently, the runtime memory overhead of a TypeScript interface is exactly 0 bytes. However, this compile-time erasure creates a critical boundary limitation: TypeScript cannot validate whether incoming HTTP JSON payloads actually conform to the declared interface at runtime. If an API returns null for a field typed as string, TypeScript will not intercept the violation until downstream code attempts an invalid method call (such as .toUpperCase()), resulting in an unhandled TypeError. Pairing interfaces with compile-time analyzers and schema boundaries is vital for enterprise stability.
compiler ast tokenization & node synthesis
The worker parses JSON locally, builds an intermediate type tree, allocates unique declaration names and renders TypeScript text. It does not use the TypeScript compiler factory or printer in the browser. Object arrays combine observed keys; mixed value types become unions, and empty arrays use unknown elements.
trade-off analysis & schema extensibility
Interfaces describe object shapes; type aliases represent arrays, primitives and unions. Neither validates runtime input. A sample cannot prove which unseen keys are required or which future values will occur; use JSON Schema or a runtime validator for an explicit contract.
specification matrix (JSON (RFC 8259) vs. TypeScript Interfaces)
| metric | JSON (RFC 8259) | TypeScript Interfaces |
|---|---|---|
| Validation Timing | Runtime (untyped object parsing) | Compile-Time (100% erased during build) |
| Bundle Size Impact | 0 KB overhead | 0 KB (transpiled away by tsc/esbuild) |
| Type Paradigm | Dynamic weakly-typed schema | Static structural subtyping |
| Ecosystem Tooling | JSON Schema, RFC 8259 | Language Server Protocol (tsserver), VS Code intellisense |
| Execution Cost | JSON.parse CPU cost | Zero runtime overhead |
handling api edge cases & compiler gotchas
common deserialization pitfalls and solutions
Null is different from missing
A null-only sample produces null, not string | null. A key absent from some object-array records becomes optional when infer optional is on. Disabling that option intentionally requires the combined keys.
[{"id":1,"note":null},{"id":2}]
// Item shape: id: number; note?: null;Empty and mixed arrays
An empty array provides no element evidence and becomes unknown[]. Mixed arrays use unions rather than tuple types.
["paid", 3, null]
// (string | number | null)[]Exact amounts and large IDs
Unsafe integers and numeric overflow return a precision error. Fractions still use approximate JavaScript numbers. Keep exact decimal amounts and large identifiers as JSON strings.
{"id":"9007199254740993","amount":"123.4500"}Property names stay intact
Keys with punctuation are quoted; reserved property names are legal TypeScript properties. Generated declaration names are made valid and unique. __proto__ is preserved using a computed property name.
export interface Example {
"user-id": string;
default: boolean;
}step-by-step integration guide
technical specification differences & end-to-end conversion workflow
1Paste representative API data
The sample includes two invoices so a missing note can be distinguished from an explicit null. Keep exact amounts as strings.
{"invoices":[{"id":"inv_1","amount":"123.4500","note":null},{"id":"inv_2","amount":"4.0000"}],"tags":["paid",3,null],"empty":[]}2Review inferred fields
With infer optional on, note is optional and null-only; it is not widened to an unobserved string type.
export interface Invoice {
id: string;
amount: string;
note?: null;
}3Use the exported root type
RootPayload refers to Invoice[] and retains mixed and empty-array behavior. The example output below the editor is generated from the same input.
export interface RootPayload {
invoices: Invoice[];
tags: (string | number | null)[];
empty: unknown[];
}4Compile and validate the boundary
Compile the types in your project. TypeScript types are erased at runtime; use a validator such as Zod if incoming data must be checked.
npx tsc --noEmit --strict src/types/api-contracts.ts# Run TypeScript compiler verification on generated interfaces
npx tsc --noEmit --strict src/types/api-contracts.tsfrequently asked questions
common developer questions answered
q:Should I use 'interface' or 'type' for deserialized JSON API payloads?
The converter emits interfaces for object declarations and type aliases for root arrays, primitives or unions. Choose the shape that matches your data; both are compile-time types rather than runtime validators.
// Interface declaration merging example:
interface UserResponse {
id: string;
}
interface UserResponse {
roles: string[]; // Merged automatically into { id: string; roles: string[]; }
}q:How does the converter handle nested polymorphic objects in JSON arrays?
Object arrays combine observed keys. With infer optional on, keys missing in any sampled record become optional. Divergent primitive values become unions such as string | number, not string literal enums.
// Polled multi-item array inference:
const items = [{ id: 1, tag: 'prod' }, { id: 2, meta: { active: true } }];
// Yields interface with tag?: string; meta?: { active: boolean };q:Why does JSON numeric parsing default to 'number' instead of 'bigint'?
JSON standard RFC 8259 does not specify numeric bit widths, but standard JavaScript JSON.parse parses all numbers as IEEE 754 floats. Emitting 'bigint' requires custom revivers or lossless JSON parsers. If your JSON contains 64-bit integer strings, consider annotating those specific fields as bigint or string manually.
// Using custom JSON reviver for bigints:
JSON.parse(text, (key, value) => {
if (typeof value === 'string' && /^\d{16,}$/.test(value)) return BigInt(value);
return value;
});q:What can a JSON sample establish?
Inference covers observed values only. Empty arrays have unknown elements; mixed arrays use unions. Missing keys in object arrays become optional when infer optional is on; explicit null remains distinct from missing. Disabling infer optional intentionally requires the combined keys. JSON strings remain strings in TypeScript and Zod. Numbers use JavaScript IEEE 754 precision: unsafe integers and overflow fail explicitly; fractional numbers can still round. Encode exact decimals and large integers as strings. Zod 3 cannot preserve __proto__ keys and generation fails clearly for those keys. Inferred Zod objects strip unrecognized keys by default; edit the schema if passthrough or strict keys are needed.