@fibiorg/jsontypegen
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@fibiorg/jsontypegen

@fibiorg/jsontypegen generates a Typescript type from a provided JSON input.

Check the Demo.

Install

npm install @fibiorg/jsontypegen

Usage

import { jsonToTypescript } from "@fibiorg/jsontypegen";

console.log(
    jsonToTypescript([
        { name: "Scarlet", age: 26, education: "university", friends: 3 },
        { name: "John", age: 30, friends: ["Anna", "Jackie"] },
    ])
    // Array<{"name":string;"age":number;"education"?:string;"friends":number|Array<string>;}>
);

Format with prettier

npm install prettier

In node:

import { format } from "prettier";

console.log(
    format(
        `Array<{"name":string;"age":number;"education"?:string;"friends":number|Array<string>;}>`,
        {
            parser: "typescript",
            tabWidth: 4,
            printWidth: 80,
        }
    )
);

In browser

In a browser environment, Prettier doesn't come with necessary plugins so we need to provide ones.

import * as prettier from "prettier";
import typescriptParser from "prettier/plugins/typescript";
import estreeParser from "prettier/plugins/estree";

format(
    `Array<{"name":string;"age":number;"education"?:string;"friends":number|Array<string>;}>`,
    {
        parser: "typescript",
        plugins: [typescriptParser, estreeParser],
        tabWidth: 4,
        printWidth: 80,
    }
);

Package Sidebar

Install

npm i @fibiorg/jsontypegen

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

9.78 kB

Total Files

9

Last publish

Collaborators

  • filibit