@mscottnelson/json-literal-typer
v0.0.5
Published
generates literal typescript iterfaces from JSON
Downloads
1
Readme
JSON literal typer
There are some tools out there that will produce TypeScript Interfaces from a
given JSON structure for you. But they only give you the basic types (sting
,
number
, etc.). What if there is an API where you want to get its literal
values/types?
Demo
There is a live demo at https://json-literal-typer.peppnet.de
Example
Let's assume there is an API for gas stations. It will respond with data like this:
{
"stations": [
{ "id": 1, "name": "station A", "status": "OPEN", "attributes": ["fast"] },
{
"id": 2,
"name": "station B",
"status": "OPEN",
"attributes": ["fast", "24/7"],
"operator": "Station Corp."
},
{
"id": 3,
"name": "station C",
"status": "CLOSED",
"attributes": [],
"operator": "ACME Inc."
}
]
}
What you would get from other tools is:
interface Station {
id: number;
name: string;
status: string;
attributes: string[];
operator?: string;
}
interface RootObject {
stations: Station[];
}
Nice. But what if you could get:
interface Stations {
attributes: ('24/7' | 'fast')[];
id: 1 | 2 | 3;
name: 'station A' | 'station B' | 'station C';
operator?: 'ACME Inc.' | 'Station Corp.';
status: 'CLOSED' | 'OPEN';
}
interface Root {
stations: Stations[];
}
related or inspiring projects
- json-to-ts - Convert jsons to typescript interfaces
- json2ts - generate TypeScript interfaces from JSON
- quicktype - Generate types and converters from JSON, Schema, and GraphQL
- ts-ast-viewer - TypeScript AST viewer