typing-json
v0.1.13
Published
Convert JSON data to TypeScript types
Readme
typing-json
Get types from your JSON data to use in TypeScript.
Install
npm i typing-json
yarn add typing-json
pnpm add typing-jsonUsage
CLI
npx typing-json '{
"name": "France",
"capital": "Paris",
"population": 67364357,
"area": 551695,
"currency": "Euro",
"languages": ["French"],
"region": "Europe",
"subregion": "Western Europe",
"flag": "https://upload.wikimedia.org/wikipedia/commons/c/c3/Flag_of_France.svg"
}'Result:
{
name: string;
capital: string;
population: number;
area: number;
currency: string;
languages: string[];
region: string;
subregion: string;
flag: string;
}Library
import { jsonToTypes } from 'typing-json';
jsonToTypes('{ "name": "France" }').then(data => console.log(data));
/* Result:
{
name: string;
}
*/Type mapping
| JSON value | TypeScript type |
|---|---|
| "string" | string |
| 42 | number |
| true / false | boolean |
| null | null |
| [1, 2, 3] | number[] |
| [] | unknown[] |
| { ... } | object type with inferred properties |
Keys that are not valid TypeScript identifiers (e.g. "hello-world") are preserved as quoted property names:
npx typing-json '{"hello-world": "test", "active": true}'{
"hello-world": string;
active: boolean;
}Development
pnpm run clean
pnpm run tsc
pnpm run test