Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-tsc

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-tsc

JSON-TSC A plugin that makes it easier to convert JSON to Typescript types. <img src="https://img.shields.io/npm/v/js

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

JSON-TSC

A plugin that makes it easier to convert JSON to Typescript types.

NPM version

🤹‍♂️ Preview

Features

  • 🌈 Simple - Provides the easiest API to convert JSON into Typescript interaface.
  • 🎉 Ts Supported - Support TypeScript & type checked & type inference.

Usage

npm i json-tsc -D

Import JSON_TSC into the file you want to convert.

import { JSON_TSC } from "json-tsc";

const JSON_TEST = {
  "a": 0,
  "b": {
    "c": "string",
    "d": [1, 2, 3],
    "e": {
      "f": true,
      "g": null,
    },
  },
};

const json_tsc = new JSON_TSC({
  prependWithO: true,
  sortAlphabetically: false,
  prependExport: true,
  useArrayGeneric: false,
  optionalFields: false,
  prefix: "",
});

const result = json_tsc.transform(JSON_TEST);

// result
type JSON_TSC = ORootObject;
export interface ORootObject {
  a: number;
  b: OB;
}
export interface OB {
  c: string;
  d: number[];
  e: OE;
}
export interface OE {
  f: boolean;
  g: any;
}

Config

export interface JSON_TSC_CONFIG {
  /** 
   * zh-CN: 是否在生成的类型前面加上 O
   * en: Whether to add O in front of the generated type
   */
  prependWithO: boolean
  /** 
   * zh-CN: 是否按照字母顺序排序
   * en: Whether to sort in alphabetical order
   */
  sortAlphabetically: boolean
  /** 
   * zh-CN: 是否在生成的类型前面加上 export
   * en: Whether to add export in front of the generated type
   */
  prependExport: boolean
  /** 
   * zh-CN: 是否使用 ''Array<T>'' 代替 ''T[]'
   * en: Whether to use ''Array<T>'' instead of ''T[]''
  */
  useArrayGeneric: boolean
  /** 
   * zh-CN: 是否将所有字段设置为可选
   * en: Whether to set all fields to optional
   */
  optionalFields: boolean
  /** 
   * zh-CN: 生成的类型前面的前缀
   * en: Prefix in front of generated type
   */
  prefix: string
  /** 
   * zh-CN: 生成的类型后面的后缀
   * en: Suffix behind generated type
   */
  rootObjectName: string
}

Use it Now

image

Development

You can see the preview of the following commands:

npm run build

Then you can run playground to see the result.

npm run play

If you want start the dev server, you can run:

npm run dev

License

MIT License © 2022 yzh990918

FAQs

Package last updated on 28 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc