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-tsVerified import paths — ran on the pinned version, not inferred.
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.
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.
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`).
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.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'`.
For CommonJS, use `const JsonToTS = require('json-to-ts');`. For ESM, use `import JsonToTS from 'json-to-ts';` to correctly capture the default export.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.
No dependency data recorded yet.