ts-to-io
Converts TypeScript type and interface definitions into io-ts type validators.
Usage
As a script
$ npm install -g ts-to-io
$ ts-to-io file.ts
or
$ npx ts-to-io file.ts
From code
NOTE: The validator generation is not intended to be performed at runtime. You should first generate the validators locally and then include them in the program source.
import { getValidatorsFromString } from "ts-to-io"
const sourceString = `
type Person = { name: string; age: number | null }
`
const validators = getValidatorsFromString(sourceString)
Configuration
ts-to-io supports the following config options
Key | CLI opt | Default | Description |
---|---|---|---|
followImports |
--follow-imports |
false |
output codecs for types declared in imported files |
includeHeader |
--no-include-header |
true |
omit io-ts import from the output |
Supported types
Type | Supported | TypeScript | codec |
---|---|---|---|
string | โ | string |
t.string |
number | โ | number |
t.number |
boolean | โ | boolean |
t.boolean |
null | โ | null |
t.null |
undefined | โ | undefined |
t.undefined |
void | โ | void |
t.void |
any, unknown | โ | any , unknown |
t.unknown |
array | โ | Array<A> |
t.array(A) |
record | โ | Record<K, A> |
t.record(K, A) |
object type | โ | { name: string } |
t.type({ name: t.string }) |
interface | โ | interface I { name: string } |
t.type({ name: t.string }) |
literal | โ | 'ABC' |
t.literal('ABC') |
partial | โ | Partial<{ name: string }> |
t.partial({ name: t.string }) |
readonly | โ | Readonly<A> |
- |
readonly array | โ | ReadonlyArray<A> |
- |
tuple | โ | [ A, B ] |
t.tuple([ A, B ]) |
tuple with rest | โ | [ A, B, ...C ] |
- |
union | โ | A | B |
t.union([ A, B ]) |
intersection | โ | A & B |
t.intersection([ A, B ]) |
keyof | โ | keyof M |
- |
recursive type | โ | type Node = { children: Node[] } |
- |
function | โ | type fn = () => string |
t.Function |