JSPM

  • Created
  • Published
  • Downloads 427837
  • Score
    100M100P100Q167984F
  • 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/mapOutput
  • 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

mapOutput

Changes the output type of the given runtime type

import { mapOutput } from 'io-ts-types/lib/mapOutput'
import { createOptionFromNullable } from 'io-ts-types/lib/fp-ts/createOptionFromNullable'

// Input: t.Type<Option<number>, number | null, t.mixed>
const Input = createOptionFromNullable(t.number)

const toUndefined = <A>(x: A | null): A | undefined => (x === null ? undefined : x)

// Output: t.Type<Option<number>, number | undefined, t.mixed>
const Output = mapOutput(Input, toUndefined)

assert.strictEqual(T.encode(none), undefined)
assert.strictEqual(T.encode(some(1)), 1)

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.type({
  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(Person.decode({ name: 'Giulio', age: 44 }).map(ageLens.modify(sum(1))))
// => right({ name: 'Giulio', age: 44 })