8000 GitHub - CatKeee/json-to-tstype
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

CatKeee/json-to-tstype

Repository files navigation

json-to-tstype

中文文档

A utility library for converting JSON data to TypeScript type definitions, supporting non-standard JSON formats, nested structures, and custom type naming.

Features

  • ✅ 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

Installation

# Using npm
npm install json-to-tstype

# Using yarn
yarn add json-to-tstype

# Using pnpm
pnpm add json-to-tstype

Quick Start

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[];

API

jsonToTs(jsonString, options?)

Converts a JSON string to TypeScript type definitions.

Parameters

  • jsonString: string - The JSON string to convert, supports standard and non-standard formats
  • options: JsonToTsOptions - Optional configuration options

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;
}

Examples

Handling Nested Objects

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;
};

Handling Arrays and Mixed Types

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);

Using Custom Options

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;
}

Handling Non-Standard JSON

const json = `{
  name: "Tom Wilson",
  age: 25,
  skills: ["programming", "design"],
};`;

const typeDefinition = jsonToTs(json);
console.log(typeDefinition);

Contributing

Issues and pull requests are welcome!

License

ISC

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published
0