• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    TypeScript
  • Created about 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transform TypeScript types to io-ts runtime validator codecs

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