# json-schema-to-type

> A compile-time TypeScript library that can generate the type of a JSON instance from a JSON Schema object type.

Latest version **0.3.0** (published 2019-03-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-schema-to-type
pnpm add json-schema-to-type
yarn add json-schema-to-type
bun add json-schema-to-type
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2019-03-01 |
| First published | 2019-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Tomasz Cudok |
| Maintainers | tcudok |
| Keywords | typescript, json-schema |

## Links

- npm: https://www.npmjs.com/package/json-schema-to-type
- Repository: https://github.com/tcudok/json-schema-to-type
- npm.io page: https://npm.io/package/json-schema-to-type

## 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.3.0 (latest) — 2019-03-01
- 0.2.0 — 2019-02-24
- 0.1.1 — 2019-02-24
- 0.1.0 — 2019-02-24

## README

[![npm version](https://badge.fury.io/js/json-schema-to-type.svg)](https://badge.fury.io/js/json-schema-to-type)

# json-schema-to-type

A compile-time TypeScript library that can generate the type of a JSON instance from a JSON Schema object type.

Requires TypeScript >=3.4

## Installation

```bash
npm install -D json-schema-to-type
```

```bash
yarn -D add json-schema-to-type
```

## Usage

```typescript
import { JsonSchemaToType } from 'json-schema-to-type';

const schema = {
  type: 'object',
  properties: {
    firstName: {
      type: 'string',
    },
    lastName: {
      type: 'string',
    },
    age: {
      type: 'integer',
    },
    address: {
      type: 'object',
      properties: {
        addressLine1: {
          type: 'string',
        },
        addressLine2: {
          type: 'string',
        },
        postCode: {
          type: 'string',
        },
      },
      required: ['addressLine1', 'postCode'],
    },
    phoneNumbers: {
      type: 'array',
      items: {
        type: ['string', 'object'],
        properties: {
          areaCode: { type: 'number' },
          localNumber: { type: 'number' },
        },
        required: ['localNumber'],
      },
    },
  },
  required: ['firstName', 'lastName', 'phoneNumbers'],
} as const;

type Type = JsonSchemaToType<typeof schema>;
```

The resulting type of `Type` will be equivalent to:

```typescript
type Type = {
  firstName: string;
  lastName: string;
  age?: number;
  address?: {
    addressLine1: string;
    addressLine2?: string;
    postCode: string;
  };
  phoneNumbers: (string | { areaCode?: number; localNumber: number })[];
};
```

## Development status

The support for many JSON Schema features is still missing. Things to implement:

- [x] `array` type
  - [x] single schema for all items
    - [x] full support for `array` schemas as item schemas (currently resolves to [][])
    - [x] full support for `object` schemas as item schemas (currently resolves to object[])
  - [ ] multiple items schemas (tuples)
  - [ ] `additionalItems` property
  - [ ] `contains` property
- [x] `object` type
  - [x] required properties
  - [ ] `patternProperties` property
  - [ ] `additionalProperties` property
- [x] multi-type schemas
  - [x] full support for `array` schemas in multi-type schemas (currently resolves to [])
  - [x] full support for `object` schemas in multi-type schemas (currently resolves to object)
- [ ] `enum` property
- [ ] `const` property
- [ ] `definitions` property (investigate `$ref` support)
- [ ] `if`, `then`, `else` properties
- [ ] `allOf`, `anyOf`, `oneOf`, `not` properties

---
_Source: https://npm.io/package/json-schema-to-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
