Skip to content

pedily/json-typings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-typings

Generate TypeScript type definitions from JSON-compatible JavaScript objects

Do you want to get type declarations for JSON-like JavaScript objects at runtime? Do you NOT want to ship 8MB of TypeScript as a dependency to do so? Then this might be what you are looking for!

Usage

TL;DR: take a JSON-like value and pass it into getDeclaration, get back its typings.

const { getDeclaration } = require('json-typings')

const person = {
    firstname: 'John',
    lastname: 'Doe',
    luckyNumber: 42,
    awesome: true,
    children: [
        'Jane',
        'Max'
    ]
}

console.log(getDeclaration(person));

outputs (without linebreaks and spaces)

{
    firstname: string;
    lastname: string;
    luckyNumber: number;
    awesome: bolean;
    chlidren: string[];
}

The above output can be used to describe this object as a TypeScript declaration. You can add it to a .d.ts file like this:

declare const person = { firstname: string; /* ... */ };

API

const { getDeclaration } = require('json-typings')

Install

With npm installed, run

$ npm install json-typings

License

MIT