0.3.1 • Published 4 years ago

ts-to-io v0.3.1

Weekly downloads
25
License
MIT
Repository
github
Last release
4 years ago

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

KeyCLI optDefaultDescription
followImports--follow-importsfalseoutput codecs for types declared in imported files
includeHeader--no-include-headertrueomit io-ts import from the output

Supported types

TypeSupportedTypeScriptcodec
stringstringt.string
numbernumbert.number
booleanbooleant.boolean
nullnullt.null
undefinedundefinedt.undefined
voidvoidt.void
any, unknownany, unknownt.unknown
arrayArray<A>t.array(A)
recordRecord<K, A>t.record(K, A)
object type{ name: string }t.type({ name: t.string })
interfaceinterface I { name: string }t.type({ name: t.string })
literal'ABC't.literal('ABC')
partialPartial<{ name: string }>t.partial({ name: t.string })
readonlyReadonly<A>-
readonly arrayReadonlyArray<A>-
tuple[ A, B ]t.tuple([ A, B ])
tuple with rest[ A, B, ...C ]-
unionA \| Bt.union([ A, B ])
intersectionA & Bt.intersection([ A, B ])
keyofkeyof M-
recursive typetype Node = { children: Node[] }-
functiontype fn = () => stringt.Function
0.3.1

4 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago