Git Product home page Git Product logo

ts-to-io's Introduction

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

ts-to-io's People

Contributors

dependabot[bot] avatar juusaw avatar tkurki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.