1.9.1 • Published 9 months ago

gen-ts-type v1.9.1

Weekly downloads
9
License
BSD-2-Clause
Repository
github
Last release
9 months ago

gen-ts-type

Generate Typescript type from sample data

npm Package Version

Installation

npm i -g gen-ts-type

Usage

From cli

echo 'export type Package = ' | tee package.d.ts
format=1 allowEmptyArray=1 allowMultiTypedArray=1 gen-ts-type package.json | tee -a package.d.ts

From typescript

import { genTsType } from 'gen-ts-type';
import * as fs from 'fs';

const UserType = genTsType(
    { user: 'Alice', friends: [{ user: 'Bob', since: new Date() }] },
    { format: true },
  );
const code = `export type User = ${UserType};`
fs.writeFileSync('types.ts', code);

Above example generate into:

export type User = {
  user: string;
  friends: Array<{
    user: string;
    since: Date;
  }>;
}

Features

Supported features

  • primitive types
    • string
    • number
    • boolean
    • bigint
    • Date
    • symbol (not specific)
  • Array (single-type / union-type)
  • Object (strict-type / optional-type)
  • named custom type

Todo features

  • Tuple (specific-type array) (e.g. [number, string, string])
  • specific symbol / string / number
  • Enum
1.9.1

9 months ago

1.9.0

9 months ago

1.8.0

2 years ago

1.7.1

4 years ago

1.7.0

5 years ago

1.6.1

5 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago