Command line utility to emit TypeScript type declaration files for JSON files powered by
npm install -g dts-from-json
dts-from-json <json-file>
You can pass a local path or URL as <json-file>
.
The types are printed in stdout, to save to a file simply pipe to output to a file:
dts-from-json <json-file> > <dts-dir>
dts-from-json demo/json/senators.json
dts-from-json https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json
dts-from-json demo/json/senators.json > demo/types/senators.d.ts
dts-from-json demo/json/senators.json --root Senators
dts-from-json ./demo/json/senators.json --root Senators --types true
npx dts-from-json <json-file>
The default root type is the CamelCase version of the json file name. For example, if your file is called nobe-prize.d.ts
the root type will be NobelPrice
by default.
$ dts-from-json <json-file> --root Senators
$ dts-from-json <json-file> --types
- Using json properties with names that have collisions with other types will cause issues. For example, if you have json that looks like:
{
"meta": {
// ...
},
"objects": [
// ...
]
}
It will be translated into:
interface Meta {
// ...
}
interface Object { // <-- Collision with Object type
// ...
}
interface RootObject {
meta: Meta;
objects: Object[];
}
This will lead to problems with the already defined Object
type.