1.0.1 • Published 1 year ago
dts-from-json v1.0.1
dts-from-json
Command line utility to emit TypeScript type declaration files for JSON files powered by
Installation and usage
npm install -g dts-from-jsondts-from-json <json-file>You can pass a local path or URL as <json-file>.
Save to a 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>Examples
dts-from-json demo/json/senators.jsondts-from-json https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.jsondts-from-json demo/json/senators.json > demo/types/senators.d.ts dts-from-json demo/json/senators.json --root Senatorsdts-from-json ./demo/json/senators.json --root Senators --types trueUsage via npx
npx dts-from-json <json-file>Customize the root type
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 SenatorsUse type instead of interface
$ dts-from-json <json-file> --typesKnown limitations
- 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.