Registry / type-stubs / json-to-ts

json-to-ts

JSON →
library2.1.0jsnpmunverified

json-to-ts is a utility library, currently at stable version 2.1.0, designed to automatically convert JSON objects into corresponding TypeScript interface definitions. It parses input JSON data, intelligently inferring primitive types (strings, numbers, booleans), array structures, and nested objects. Key differentiators include its robust handling of complex scenarios like automatic array type merging, generation of union types where data properties might vary, and prevention of redundant type declarations, ensuring clean and efficient output. It also offers the capability to infer optional properties. This package aims to streamline the developer workflow by reducing the manual effort and potential for errors associated with defining TypeScript types for API responses or dynamic data structures. It follows an active release cadence, providing reliable tooling for TypeScript projects dealing with structured JSON.

npm install json-to-ts
INSTALL
IMPORT
SIG · JSON-TO-TS
J
json-to-ts
type-stubsjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JsonToTS
import JsonToTS from 'json-to-ts';
import { JsonToTS } from 'json-to-ts';
The primary conversion function is exported as the default export in ESM. Attempting a named import will likely result in an undefined function.
JsonToTS (CommonJS)
const JsonToTS = require('json-to-ts');
For CommonJS environments, the entire module export is the JsonToTS function.
Type definitions
import type { RootObject, Cat } from 'json-to-ts/dist/types';
While `json-to-ts` generates types, if you need to import its internal types (e.g., for extending its behavior), they are typically found in the `dist/types` directory. However, generally, you'd use the generated types directly.

This example demonstrates how to import and use `json-to-ts` to convert a complex JSON object into a set of TypeScript interfaces, including nested objects and optional properties.

import JsonToTS from 'json-to-ts'; const json = { users: [ { id: 1, name: 'Alice', email: 'alice@example.com' }, { id: 2, name: 'Bob', email: 'bob@example.com', phone: '123-456-7890' } ], settings: { theme: 'dark', notificationsEnabled: true }, version: 2.1, timestamp: new Date().toISOString() }; console.log('Generating TypeScript interfaces...'); JsonToTS(json, { rootName: 'AppData', // Optional: customize type names, e.g., for arrays of specific objects // arrayTypeSuffix: 'Item' }).forEach(typeInterface => { console.log(typeInterface); }); /* Expected Output: interface AppData { users: User[]; settings: Settings; version: number; timestamp: string; } interface User { id: number; name: string; email: string; phone?: string; } interface Settings { theme: string; notificationsEnabled: boolean; } */
json-to-ts --version
Debug
Known issues
gotchaGenerating types for very large or deeply nested JSON structures can strain TypeScript's type inference engine, potentially leading to slow compilation times or 'Type instantiation is excessively deep and potentially infinite' errors, particularly with older TypeScript versions or specific compiler flags.
fix
For extremely complex or large JSONs, consider simplifying the input or manually refining the generated types. Ensure your `tsconfig.json` has appropriate `maxNodeModuleJsDepth` or similar settings if encountering deep recursion errors.
affects: >=1.0.0
gotchaWhile `json-to-ts` excels at inference, highly ambiguous JSON structures (e.g., arrays with mixed types, objects with inconsistent property presence across array elements, or null values without explicit alternatives) might result in broader union types or `any` where more specific types were desired. This sometimes requires manual refinement of the generated interfaces.
fix
Review the generated types for `any` or overly broad union types. Manually specify more precise types if the inference is not exact enough for your use case, particularly for optional fields (e.g., `field?: Type`) or explicit nullability (e.g., `field: Type | null`).
affects: >=1.0.0
gotchaThe library primarily uses a CommonJS default export (`module.exports = function`). When used in a pure ESM project, especially with strict `esModuleInterop: false` or certain bundler configurations, directly importing `JsonToTS` might behave unexpectedly if not using `import JsonToTS from 'json-to-ts'` (default import syntax).
fix
Ensure `esModuleInterop: true` in your `tsconfig.json` for better compatibility. Always use the default import syntax (`import JsonToTS from 'json-to-ts';`) for ESM, and `const JsonToTS = require('json-to-ts');` for CommonJS.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'json-to-ts'
The package is not installed or the import path is incorrect.
fix
Run `npm install json-to-ts` or `yarn add json-to-ts`. Verify the import statement uses the correct package name: `from 'json-to-ts'`.
TypeError: JsonToTS is not a function
The main conversion function was imported incorrectly, often by attempting a named import from a module that uses a default or CommonJS module.exports export.
fix
For CommonJS, use `const JsonToTS = require('json-to-ts');`. For ESM, use `import JsonToTS from 'json-to-ts';` to correctly capture the default export.
Property 'x' is missing in type 'Y' but required in type 'Z' (or similar type mismatch during compilation/runtime)
The TypeScript interfaces generated by `json-to-ts` do not perfectly align with the actual runtime JSON data, possibly due to optional fields not being inferred correctly or unexpected variations in data structure.
fix
Manually review and adjust the generated TypeScript interfaces to precisely reflect the expected JSON structure. Add `?` for optional properties or use union types (e.g., `string | number | null | undefined`) where data can vary. Ensure sample JSON provided for generation is representative of all possible data shapes.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources
json-to-ts — npm install json-to-ts · libregistry