Generate runtypes from static types & JSON schema.
npm install runtypes
npm install -D runtyping
- Create a file, in the root of your project, called "runtyping.yml".
# runtyping.yml
# This config represents a list of files to create.
- targetFile: src/runtypes.ts # The file to create
sourceTypes:
- file: src/types.ts # The file where your type lives
type: Foo # The type you want to convert to a runtype
- file: json/my-json-schema.json # You can even use JSON schema files!!
type: [ExampleType, AnotherExampleType]
- Then run:
npx runtyping
Basic example:
import { Generator } from 'runtyping'
const generator = new Generator({
targetFile: 'src/runtypes.ts',
})
generator
.generate([
{ file: 'src/types.ts', type: 'Foo' },
{ file: 'json/my-json-schema.json', type: 'ExampleType' },
])
.then((file) => file.save())
You can also pass a custom tsconfig file:
const generator = new Generator({
targetFile: 'src/runtypes.ts',
tsConfigFile: '/path/to/tsconfig.json',
})
...or a custom ts-morph project (for the internal compiler):
(see generate.ts for the defaults)
import { Project } from 'ts-morph'
const generator = new Generator({
targetFile: 'src/runtypes.ts',
project: new Project({
// ...
}),
})
Inspired by a gist by skurfuerst.