Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

compile json schema to typescript typings

NotificationsYou must be signed in to change notification settings

jdrew1303/json-schema-to-typescript

 
 

Repository files navigation

Compile json schema to typescript typings

[In Beta]: Bug reports appreciated!

Example

Input:

{"title":"Example Schema","type":"object","properties": {"firstName": {"type":"string"    },"lastName": {"type":"string"    },"age": {"description":"Age in years","type":"integer","minimum":0    },"hairColor": {"enum": ["black","brown","blue"],"type":"string"    }  },"required": ["firstName","lastName"]}

Output:

exportinterfaceExampleSchema{firstName:string;lastName:string;/**   * Age in years   */age?:number;hairColor?:"black"|"brown"|"blue";}

Installation

npm install json-schema-to-typescript

Usage

import{compileFromFile}from'json-schema-to-typescript'fs.writeFileSync('dist/foo.d.ts',awaitcompileFromFile('src/foo.json'))

See/example for a fully working demo.

CLI

Simple CLI utility is provided within the package.

cat foo.json | json2ts > foo.d.ts# orjson2ts foo.json# orjson2ts foo.json foo.d.ts# orjson2ts --input foo.json --output foo.d.ts# orjson2ts -i foo.json -o foo.d.ts

In case you don't specify output path, results are saved in input directory in.d.ts file.

Tests

npm test

Todo

  • title =>interface
  • Primitive types:
    • array
    • homogeneous array
    • boolean
    • integer
    • number
    • null
    • object
    • string
    • homogeneous enum
    • heterogeneous enum
  • Non/extensible interfaces
  • Custom JSON-schema extensions
  • Nested properties
  • Schema definitions
  • Schema references
  • Local (filesystem) schema references
  • External (network) schema references
  • Add support for running in browser
  • default interface name
  • infer unnamed interface name from filename
  • anyOf ("union")
  • allOf ("intersection")
  • additionalProperties of type
  • extends
  • required properties on objects (eg)
  • validateRequired (eg)
  • literal objects in enum (eg)
  • referencing schema by id (eg)
  • clean up + refactor code

Not expressible in TypeScript:

Further Reading

About

compile json schema to typescript typings

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript94.5%
  • JavaScript5.5%

[8]ページ先頭

©2009-2025 Movatter.jp