# @puffmeow/schema2ts

> Transform JSON Schema to Typescript interface automatically

Latest version **0.2.4** (published 2023-08-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install @puffmeow/schema2ts
pnpm add @puffmeow/schema2ts
yarn add @puffmeow/schema2ts
bun add @puffmeow/schema2ts
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2023-08-10 |
| First published | 2023-03-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | ji_quan |
| Keywords | jsonschema to interface, jsonschema to ts, schema to typescript, schema to ts, schema to interface, schema to type |

## Links

- npm: https://www.npmjs.com/package/@puffmeow/schema2ts
- Repository: https://github.com/PuffMeow/schema2ts
- Homepage: https://github.com/PuffMeow/schema2ts#readme
- Issues: https://github.com/PuffMeow/schema2ts/issues
- npm.io page: https://npm.io/package/@puffmeow/schema2ts

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.2.4 (latest) — 2023-08-10
- 0.2.3 — 2023-08-09
- 0.2.2 — 2023-08-09
- 0.2.1 — 2023-08-09
- 0.2.0 — 2023-08-08
- 0.1.3 — 2023-08-07
- 0.1.2 — 2023-08-03
- 0.1.1 — 2023-03-25
- 0.1.0 — 2023-03-25
- 0.0.2 — 2023-03-25
- 0.0.1 — 2023-03-25

## README

## Introduction

This tool can help you transform your JSON Schema to TypeScript interface quickly🧲.

[Git repository](https://github.com/PuffMeow/schema2ts). If you like it, please give me a little star⭐, thanks~

If you want a faster implementation, you can see [rusty-schema2ts](https://github.com/PuffMeow/rusty-schema2ts), it uses napi-rs to implement and it's faster than this TypeScript version.

**The api of them are all the same.**

## TypeScript vs Rust

You can find [benchmark here](https://github.com/PuffMeow/rusty-schema2ts/blob/main/bench/index.js)

| index | Task Name             | ops/sec | Average Time (ns)  | Margin | Samples |
| ----- | --------------------- | ------- | ------------------ | ------ | ------- |
| 0     | TypeScript: schema2ts | 2,796   | 357534.31021794415 | ±1.08% | 1399    |
| 1     | Rust: rustySchema2ts  | 5,431   | 184122.05448994122 | ±0.29% | 2716    |

## Install

npm

```
npm i @puffmeow/schema2ts
```

pnpm

```
pnpm i @puffmeow/schema2ts
```

yarn

```
yarn add @puffmeow/schema2ts
```

## Quick start

It's really easy to use.

```ts
import { schema2ts } from '@puffmeow/schema2ts';

// The "options" we will introduce later
// schema2ts(schema: string, options?: IOptions): string
schema2ts(`your schema`, options);
```

## Options

| key               | type     | required | default                                          | description                                                                                                                                                                                                                                                                                  |
| ----------------- | -------- | -------- | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| preffix           | string   | ×        | I                                                | Interface preffix, if you don't like this, you can give it a empty string                                                                                                                                                                                                                    |
| preffixOfEnum     | string   | ×        | T                                                | Enum type preffix, if you don't like this, you can give it a empty string                                                                                                                                                                                                                    |
| isGenComment      | boolean  | ×        | false                                            | Whether to automatically generate comments                                                                                                                                                                                                                                                   |
| isExport          | boolean  | ×        | true                                             | Whether to export the interfaces and types                                                                                                                                                                                                                                                   |
| indent            | number   | ×        | 2                                                | Code indent                                                                                                                                                                                                                                                                                  |
| semi              | boolean  | ×        | true                                             | Is enable semicolon                                                                                                                                                                                                                                                                          |
| optional          | boolean  | ×        | true                                             | If this is enabled, it will generate the optional interface, default value is true                                                                                                                                                                                                           |
| ignoreKeys        | string[] | ×        | []                                               | If you don't want to generate the type of an attribute in a root object, you can pass in the key name of the corresponding attribute.<br /><br />Like this, ignoreKeys: ["firstName", "lastName"]<br /><br />Schema2ts will ignore the two attributes and doesn't generate the type of them. |
| explain           | string   | ×        |                                                  | Display some comments at the top of the code                                                                                                                                                                                                                                                 |
| parseErrorMessage | string   | ×        | // Parse schema error, please check your schema. | When parsing schema error, this message will be return                                                                                                                                                                                                                                       |

## Example:

### Input schema

If you have a schema like this:

```json
{
  "title": "Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "type": "number"
    },
    "hairColor": {
      "enum": [
        {
          "title": "hair color1",
          "value": "color1"
        },
        {
          "title": "hair color2",
          "value": "color2"
        },
        {
          "title": "hair color3",
          "value": "color3"
        }
      ],
      "type": "string"
    },
    "obj": {
      "type": "object",
      "properties": {
        "key1": {
          "type": "string"
        },
        "key2": {
          "type": "number"
        },
        "key3": {
          "type": "boolean"
        }
      }
    },
    "arr": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "arr1": {
            "type": "string"
          },
          "arr2": {
            "type": "number"
          },
          "arr3": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "enen1": {
                  "type": "string"
                },
                "enen2": {
                  "type": "number"
                },
                "enen3": {
                  "type": "boolean"
                },
                "enen4": {
                  "type": "unknow type will transform to any"
                }
              }
            }
          }
        }
      }
    }
  }
}
```

### Output TypeScript interface

Finally it will output like this:

```ts
export type THairColor = 'color1' | 'color2' | 'color3';

export interface ISchema {
  firstName?: string;
  lastName?: string;
  age?: number;
  hairColor?: THairColor;
  obj?: IObj;
  arr?: IArr[];
}

export interface IObj {
  key1?: string;
  key2?: number;
  key3?: boolean;
}

export interface IArr {
  arr1?: string;
  arr2?: number;
  arr3?: IArr3[];
}

export interface IArr3 {
  enen1?: string;
  enen2?: number;
  enen3?: boolean;
  enen4?: any;
}
```

## More examples

### 1.generate comment

```ts
schema2ts(`below json`, { isGenComment: true });
```

#### input json

```json
{
  "title": "Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "title": "This is the first name",
      "type": "string"
    },
    "lastName": {
      "title": "This is the last name",
      "type": "string"
    },
    "age": {
      "title": "This is the age",
      "type": "number"
    },
    "hairColor": {
      "title": "This is the hair color",
      "enum": [
        {
          "title": "hair color1",
          "value": "color1"
        },
        {
          "title": "hair color2",
          "value": "color2"
        },
        {
          "title": "hair color3",
          "value": "color3"
        }
      ],
      "type": "string"
    },
    "obj": {
      "type": "object",
      "title": "Object test",
      "properties": {
        "key1": {
          "title": "This is the key1",
          "type": "string"
        },
        "key2": {
          "title": "This is the key2",
          "type": "number"
        },
        "key3": {
          "title": "This is the key3",
          "type": "boolean"
        }
      }
    },
    "arr": {
      "type": "array",
      "title": "Arr test",
      "items": {
        "type": "object",
        "title": "Nested array items",
        "properties": {
          "arr1": {
            "title": "This is the arr1",
            "type": "string"
          },
          "arr2": {
            "title": "This is the arr2",
            "type": "number"
          },
          "arr3": {
            "type": "array",
            "title": "Test arr3",
            "items": {
              "type": "object",
              "title": "Test nested arr3 items",
              "properties": {
                "enen1": {
                  "title": "This is the enen1",
                  "type": "string"
                },
                "enen2": {
                  "title": "This is the enen2",
                  "type": "number"
                },
                "enen3": {
                  "title": "This is the enen3",
                  "type": "boolean"
                }
              }
            }
          }
        }
      }
    }
  }
}
```

#### output

```ts
export type THairColor = 'color1' | 'color2' | 'color3';

/** Schema */
export interface ISchema {
  /** This is the first name */
  firstName?: string;
  /** This is the last name */
  lastName?: string;
  /** This is the age */
  age?: number;
  /** This is the hair color */
  hairColor?: THairColor;
  /** Object test */
  obj?: IObj;
  /** Arr test Nested array items */
  arr?: IArr[];
}

/** Object test */
export interface IObj {
  /** This is the key1 */
  key1?: string;
  /** This is the key2 */
  key2?: number;
  /** This is the key3 */
  key3?: boolean;
}

/** Nested array items */
export interface IArr {
  /** This is the arr1 */
  arr1?: string;
  /** This is the arr2 */
  arr2?: number;
  /** Test arr3 Test nested arr3 items */
  arr3?: IArr3[];
}

/** Test nested arr3 items */
export interface IArr3 {
  /** This is the enen1 */
  enen1?: string;
  /** This is the enen2 */
  enen2?: number;
  /** This is the enen3 */
  enen3?: boolean;
}
```

### 2.ignoreKeys

```ts
schema2ts(`below json`, {
  ignoreKeys: ['firstName', 'obj', 'hairColor', 'arr'],
  isGenComment: true,
  optional: false,
});
```

#### input json

```json
{
  "title": "Test",
  "type": "object",
  "properties": {
    "firstName": {
      "title": "This is the first name",
      "type": "string"
    },
    "lastName": {
      "title": "This is the last name",
      "type": "string"
    },
    "age": {
      "title": "This is the age",
      "type": "number"
    },
    "hairColor": {
      "title": "This is the hair color",
      "enum": [
        {
          "title": "hair color1",
          "value": "color1"
        },
        {
          "title": "hair color2",
          "value": "color2"
        },
        {
          "title": "hair color3",
          "value": "color3"
        }
      ],
      "type": "string"
    },
    "obj": {
      "type": "object",
      "title": "Object test",
      "properties": {
        "key1": {
          "title": "This is the key1",
          "type": "string"
        },
        "key2": {
          "title": "This is the key2",
          "type": "number"
        },
        "key3": {
          "title": "This is the key3",
          "type": "boolean"
        }
      }
    },
    "arr": {
      "type": "array",
      "title": "Arr test",
      "items": {
        "type": "object",
        "title": "Nested array items",
        "properties": {
          "arr1": {
            "title": "This is the arr1",
            "type": "string"
          },
          "arr2": {
            "title": "This is the arr2",
            "type": "number"
          },
          "arr3": {
            "type": "array",
            "title": "Test arr3",
            "items": {
              "type": "object",
              "title": "Test nested arr3 items",
              "properties": {
                "enen1": {
                  "title": "This is the enen1",
                  "type": "string"
                },
                "enen2": {
                  "title": "This is the enen2",
                  "type": "number"
                },
                "enen3": {
                  "title": "This is the enen3",
                  "type": "boolean"
                }
              }
            }
          }
        }
      }
    }
  }
}
```

#### output

```ts
/** Test */
export interface ITest {
  /** This is the last name */
  lastName: string;
  /** This is the age */
  age: number;
}
```

---
_Source: https://npm.io/package/@puffmeow/schema2ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
