在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:bcherny/json-schema-to-typescript开源软件地址:https://github.com/bcherny/json-schema-to-typescript开源编程语言:TypeScript 99.9%开源软件介绍:json-schema-to-typescript
ExampleInput: {
"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"
}
},
"additionalProperties": false,
"required": ["firstName", "lastName"]
} Output: export interface ExampleSchema {
firstName: string;
lastName: string;
/**
* Age in years
*/
age?: number;
hairColor?: "black" | "brown" | "blue";
} Installation# Using Yarn:
yarn add json-schema-to-typescript
# Or, using NPM:
npm install json-schema-to-typescript --save Usageimport { compile, compileFromFile } from 'json-schema-to-typescript'
// compile from file
compileFromFile('foo.json')
.then(ts => fs.writeFileSync('foo.d.ts', ts))
// or, compile a JS object
let mySchema = {
properties: [...]
}
compile(mySchema, 'MySchema')
.then(ts => ...) See server demo and browser demo for full examples. Options
CLIA CLI utility is provided with this package. cat foo.json | json2ts > foo.d.ts
# or
json2ts foo.json > foo.d.ts
# or
json2ts foo.json foo.d.ts
# or
json2ts --input foo.json --output foo.d.ts
# or
json2ts -i foo.json -o foo.d.ts
# or (quote globs so that your shell doesn't expand them)
json2ts -i 'schemas/**/*.json'
# or
json2ts -i schemas/ -o types/ You can pass any of the options described above (including style options) as CLI flags. Boolean values can be set to false using the # generate code for definitions that aren't referenced
json2ts -i foo.json -o foo.d.ts --unreachableDefinitions
# use single quotes and disable trailing semicolons
json2ts -i foo.json -o foo.d.ts --style.singleQuote --no-style.semi Tests
Features
Custom schema properties:
Not expressible in TypeScript:
FAQJSON-Schema-to-TypeScript is crashing on my giant file. What can I do?Prettier is known to run slowly on really big files. To skip formatting and improve performance, set the Further Reading
Who uses JSON-Schema-to-TypeScript?
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论