https://github.com/gcanti/io-ts-codegen
Code generation for io-ts
https://github.com/gcanti/io-ts-codegen
code-generation io-ts typescript
Last synced: about 1 month ago
JSON representation
Code generation for io-ts
- Host: GitHub
- URL: https://github.com/gcanti/io-ts-codegen
- Owner: gcanti
- License: mit
- Created: 2017-03-05T17:59:18.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-24T14:31:06.000Z (about 2 years ago)
- Last Synced: 2024-10-12T22:55:29.630Z (7 months ago)
- Topics: code-generation, io-ts, typescript
- Language: TypeScript
- Homepage: https://gcanti.github.io/io-ts-codegen/
- Size: 479 KB
- Stars: 156
- Watchers: 8
- Forks: 14
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/buildo/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.
```ts
import * as gen from 'io-ts-codegen'// list of type declarations
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)])
)
]// apply topological sort in order to get the right order
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)
```ts
const Person = t.type({
name: t.string,
age: t.number
})
const Persons = t.array(Person)
interface Person {
name: string
age: number
}
type Persons = Array>
```# Documentation
- [API Reference](https://gcanti.github.io/io-ts-codegen)