Motivation
Generate both static and runtime types from an intermediate language.
The intermediate language can in turn be generated from other schemas: JSON Schema, Swagger, metarpheus, etc..
io-ts
compatibility
io-ts-codegen version | target io-ts version |
---|
0.4.0+ | 1.0.0+ || 2.0.0+ |
0.3.0+ | 1.0.0+ || 2.0.0+ |
Usage
Nodes of the intermediate language can be built from the provided builders.
import * as gen from 'io-ts-codegen'
const declarations = [
gen.typeDeclaration('Persons', gen.arrayCombinator(gen.identifier('Person'))),
gen.typeDeclaration(
'Person',
gen.typeCombinator([gen.property('name', gen.stringType), gen.property('age', gen.numberType)])
)
]
const sorted = gen.sort(declarations)
console.log(sorted.map(d => gen.printRuntime(d)).join('\n'))
console.log(sorted.map(d => gen.printStatic(d)).join('\n'))
Output (as string)
const Person = t.type({
name: t.string,
age: t.number
})
const Persons = t.array(Person)
interface Person {
name: string
age: number
}
type Persons = Array<t.TypeOf<typeof Person>>
Documentation