JSPM

  • Created
  • Published
  • Downloads 433456
  • Score
    100M100P100Q177886F
  • License MIT

A collection of runtime types for use with io-ts

Package Exports

  • io-ts-types
  • io-ts-types/lib/fp-ts/createOptionFromNullable
  • io-ts-types/lib/newtype-ts/fromNewtype
  • io-ts-types/lib/number/IntegerFromString

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (io-ts-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

A collection of runtime types and combinators for use with io-ts

API

newtype-ts

fromNewtype

Given

import { Newtype, iso } from 'newtype-ts'

type Age = Newtype<'Age', number>

I want to define a runtime type whose derived type is

type Person = {
  name: string
  age: Age
}

Solution

import * as t from 'io-ts'
import { fromNewtype } from 'io-ts-types/lib/newtype-ts/fromNewtype'

const Person = t.interface({
  name: t.string,
  age: fromNewtype<Age>(t.Integer)
})

Usage example

import { iso } from 'newtype-ts'
import { Lens } from 'monocle-ts'

type Person = t.TypeOf<typeof Person>

const ageLens = Lens.fromProp<Person, 'age'>('age').composeIso(iso<Age>())

const sum = (a: number) => (b: number) => a + b

console.log(t.validate({ name: 'Giulio', age: 43 }, Person).map(ageLens.modify(sum(1))))
// => right({ name: 'Giulio', age: 44 })