io-ts-types
Version:
A collection of codecs and combinators for use with io-ts
29 lines (28 loc) • 928 B
TypeScript
/**
* @since 0.5.2
*/
import { AnyNewtype, CarrierOf } from 'newtype-ts'
import * as t from 'io-ts'
/**
* Returns a codec from a newtype
*
* @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)'])
*
* @since 0.5.2
*/
export declare function fromNewtype<N extends AnyNewtype = never>(
codec: t.Type<CarrierOf<N>, t.OutputOf<CarrierOf<N>>>,
name?: string
): t.Type<N, CarrierOf<N>, unknown>