typescript-generate
v0.5.0
Published
Types and Interfaces generator
Downloads
7
Readme
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