A utility library for converting JSON data to TypeScript type definitions, supporting non-standard JSON formats, nested structures, and custom type naming.
- ✅ Supports standard JSON and non-standard JSON (unquoted keys, trailing commas, trailing semicolons, etc.)
- ✅ Intelligently handles nested objects and array structures
- ✅ Automatically generates meaningful type names
- ✅ Supports custom root type naming
- ✅ Supports using interfaces instead of type aliases
- ✅ Supports optional properties
- ✅ Formatted output with configurable indentation
# Using npm
npm install json-to-tstype
# Using yarn
yarn add json-to-tstype
# Using pnpm
pnpm add json-to-tstype
import { jsonToTs } from "json-to-tstype";
// Simple example
const json = `{
"name": "John Doe",
"age": 30,
"isActive": true,
"skills": ["TypeScript", "React", "Node.js"]
}`;
const typeDefinition = jsonToTs(json);
console.log(typeDefinition);
Output:
type RootObject = {
name: string;
age: number;
isActive: boolean;
skills: SkillsArray;
};
type SkillsArray = string[];
Converts a JSON string to TypeScript type definitions.
jsonString
:string
- The JSON string to convert, supports standard and non-standard formatsoptions
:JsonToTsOptions
- Optional configuration options
interface JsonToTsOptions {
/**
* Root type name prefix, defaults to "Root"
*/
rootName?: string;
/**
* Whether to format the output, defaults to true
*/
format?: boolean;
/**
* Number of spaces for indentation, defaults to 2
*/
indentSpaces?: number;
/**
* Whether to use interfaces instead of type aliases, defaults to false
*/
useInterface?: boolean;
/**
* Whether to mark properties as optional, defaults to false
*/
optionalProperties?: boolean;
}
const json = `{
"user": {
"profile": {
"name": "Jane Smith",
"email": "jane@example.com"
},
"settings": {
"theme": "dark",
"notifications": true
}
}
}`;
const typeDefinition = jsonToTs(json);
console.log(typeDefinition);
Output:
type RootObject = {
user: UserObject;
};
type UserObject = {
profile: ProfileObject;
settings: SettingsObject;
};
type ProfileObject = {
name: string;
email: string;
};
type SettingsObject = {
theme: string;
notifications: boolean;
};
const json = `{
"users": [
{ "id": 1, "name": "John Doe" },
{ "id": 2, "name": "Jane Smith" }
],
"mixedData": [1, "string", true, { "key": "value" }]
}`;
const typeDefinition = jsonToTs(json);
console.log(typeDefinition);
const json = `{
"name": "Tom Wilson",
"age": 25,
"address": {
"city": "New York",
"zipCode": "10001"
}
}`;
const typeDefinition = jsonToTs(json, {
rootName: "Person", // Custom root type name prefix
useInterface: true, // Use interfaces instead of type aliases
optionalProperties: true, // Mark properties as optional
indentSpaces: 4, // Use 4 spaces for indentation
});
console.log(typeDefinition);
Output:
interface PersonObject {
name?: string;
age?: number;
address?: AddressObject;
}
interface AddressObject {
city?: string;
zipCode?: string;
}
const json = `{
name: "Tom Wilson",
age: 25,
skills: ["programming", "design"],
};`;
const typeDefinition = jsonToTs(json);
console.log(typeDefinition);
Issues and pull requests are welcome!