typescript-generate

0.5.0 • Public • Published

typesript-generate

Custom TypeScript transformer that allows automatic deep type analysis and type-based data generation

Usage

To add typescript-generate to your project, you need to install ttypescript (https://github.com/cevek/ttypescript)

yarn add -D ttypescript typescript-generate

Then, add typescrupt-generate transfomer to your tsconfig.json:

    "plugins": [
        { "transform": "./node_modules/typescript-generate/dist/transform-inline/transformer" }
    ]

After that you should be able to type

import { generate } from 'typescript-generate'

export type MyType = ...

// If MyType is a primitive type
export generateMyType = () => generate<MyType>()

// If MyType is a reference type
export generateMyType = (value?: Partial<MyType>) => generate<MyType>()

Examples

Date

// Equivalent of new Date()
generate<Date>()

type DateType = {
    dateField: Date
}
// Returns { dateField: new Date() }
generate<DateType>()


type MixedDateType = {
    dateField: Date
    stringField: string
}
// Returns { stringField: 'value', dateField: new Date() }
generate<DateType>({ stringField: 'value'})

TODO

  • Provide Partial in generate(prop: Partial = {}) in order to create custom Data mock
  • Make sure that type intersection & works properly
  • Organise the tests in a concise way
  • Provide more examples in README.md

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.5.00latest

Version History

VersionDownloads (Last 7 Days)Published
0.5.00
0.4.00
0.3.00
0.2.00
0.1.00

Package Sidebar

Install

npm i typescript-generate

Weekly Downloads

0

Version

0.5.0

License

MIT

Unpacked Size

85.1 kB

Total Files

25

Last publish

Collaborators

  • firellon