1.0.2 • Published 6 months ago

@fibiorg/jsontypegen v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@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,
    }
);
1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago