Skip to content

Repository files navigation

typing-json

JavaScript TypeScript NodeJS PNPM Jest

Get types from your JSON data to use in TypeScript.

Install

npm i typing-json
yarn add typing-json
pnpm add typing-json

Usage

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

Example

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

License

MIT

About

Get types from your JSON data to use in TypeScript

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages