Skip to main content Link Menu Expand (external link) Document Search Copy Copied

fromNewtype overview

Added in v0.5.2


Table of contents


fromNewtype

Returns a codec from a newtype

Signature

export function fromNewtype<N extends AnyNewtype = never>(
  codec: t.Type<CarrierOf<N>, t.OutputOf<CarrierOf<N>>>,
  name = `fromNewtype(${codec.name})`
): t.Type<N, CarrierOf<N>, unknown> { ... }

Example

import { fromNewtype } from 'io-ts-types/lib/fromNewtype'
import * as t from 'io-ts'
import { right } from 'fp-ts/lib/Either'
import { PathReporter } from 'io-ts/lib/PathReporter'
import { Newtype, iso } from 'newtype-ts'

interface Token extends Newtype<{ readonly Token: unique symbol }, string> {}

const T = fromNewtype<Token>(t.string)

assert.deepStrictEqual(T.decode('sometoken'), right(iso<Token>().wrap('sometoken')))
assert.deepStrictEqual(PathReporter.report(T.decode(42)), ['Invalid value 42 supplied to : fromNewtype(string)'])

Added in v0.5.2