Developer Tools· 5 min read

Infer TypeScript Interfaces from Any JSON Sample with Nested Types

You will learn how the type inference engine handles nested objects, arrays, unions, nullables, and special key names to produce production-ready interfaces.

By EasyDevTools Team Last updated: 2026-08-24

Why this matters

TypeScript's type system is only as good as the interfaces you write. When you receive a JSON payload from an API, a configuration file, or a database export, manually writing an interface that matches its structure is error-prone and tedious. A single misspelled property name or wrong type annotation can compile without errors but fail at runtime. Automating this step with a JSON-to-TypeScript converter eliminates the manual translation between data and types, letting you focus on writing logic instead of typing property definitions.

The inference engine in this tool recursively walks the JSON structure and assigns types based on the observed values. Strings become `string`, integers become `number`, booleans become `boolean`, and nulls trigger an optional flag on the property. Nested objects become inline types or separate interfaces depending on depth, and arrays are typed based on their element types. When an array contains mixed types like strings and numbers, the tool produces a union type such as `(string | number)[]`. This mirrors how TypeScript itself handles heterogeneous data and gives you accurate types without manual annotation.

One of the trickier aspects of JSON-to-TypeScript conversion is handling keys that are not valid JavaScript identifiers. Property names with spaces, hyphens, or leading numbers need to be quoted in the interface definition. The tool detects these cases automatically and wraps the key in quotes only when necessary, producing clean output for well-formed JSON and correctly escaped output for edge-case keys. You can also set a custom interface name instead of accepting the default, which is useful when you need the generated type to match a specific naming convention in your codebase.

See it in action

Reference table

JSON valueInferred TypeScript type
String`string`
Integer or float`number`
Boolean`boolean`
NullOptional field with `?` modifier
Array of one type`type[]`
Array of mixed types`(type1type2)[]`
Nested objectInline type with recursive inference

How to use it

Paste your JSON sample into the input area, whether it is a single object, an array, or a nested structure.

Optionally change the interface name from the default to match your project naming convention.

Click the generate button to run the inference engine and produce the TypeScript interface.

Copy the output and paste it directly into your TypeScript file.

Testing your result

Paste the generated interface into a TypeScript file and create a variable of that type. Assign your original JSON data (parsed with JSON.parse) to the variable and check whether the compiler reports any type errors. If the JSON sample contained optional fields marked with null, verify that the interface correctly uses the question mark modifier. Test edge cases by adding an extra property to the object and confirming that TypeScript flags it as an error, which proves the interface is strict enough to catch mismatched data.

Common mistakes

Using a JSON sample that is too small, such as an object with only one property, which limits the inference engine's ability to detect optional or union types.

Forgetting that the tool infers types from the specific sample provided, not from a schema or documentation.

Not renaming the interface from the default, which can cause naming conflicts when you generate multiple interfaces in the same file.

Assuming the tool handles JSON Schema validation keywords like `enum` or `pattern`, which are not part of standard JSON data.

Edge cases and options

Empty arrays in the JSON sample are typed as `never[]` or `any[]` depending on the implementation, because there are no elements to infer the element type from. If your sample contains an empty array, consider replacing it with a representative sample array before running the inference. Date strings are inferred as `string` rather than `Date`, because JSON has no native date type and the tool cannot distinguish a date string from any other string without a schema. For keys with special characters like spaces or dashes, the tool automatically wraps them in quotes so the generated TypeScript is syntactically valid.

Real-world use cases

Generating interfaces for REST API response payloads without manual type transcription.

Creating TypeScript types from a large JSON configuration file to catch typos and missing fields at compile time.

Bootstrapping type definitions for a new project based on example data from a backend team.

Converting legacy JavaScript projects to TypeScript by generating types for existing JSON data structures.

Frequently asked questions

Q: How are nested objects handled?

A: Recursively — each nested object becomes its own inline type. Arrays of mixed types produce a union, such as `(string | number)[]`.


Q: What about optional fields?

A: Fields with null values, or arrays that are empty in the sample, are marked optional with the `?` modifier.


Q: Are string keys quoted?

A: Only when they contain characters that are not valid JavaScript identifiers, such as spaces, hyphens, or leading numbers.


Q: Can it infer Date types from date strings?

A: No — JSON has no native date type, so date strings are inferred as `string`. You would need to manually change those to `Date` in the generated interface.


Q: What happens with an empty JSON array as input?

A: An empty array has no elements for type inference, so the result may be `any[]` or `never[]`. Provide a sample with at least one element for meaningful output.

Start using it now

Try the JSON to TypeScript tool. See also JSON Formatter, JSON Schema Generator, and JSON to CSV.

Need help using this tool?

Read our complete JSON to TypeScript tutorial for step-by-step guidance.

Ready to try the tool?

No accounts. No uploads. No limits. Start now.