UNPKG

928 BTypeScriptView Raw
1/**
2 * @since 0.5.2
3 */
4import { AnyNewtype, CarrierOf } from 'newtype-ts'
5import * as t from 'io-ts'
6/**
7 * Returns a codec from a newtype
8 *
9 * @example
10 * import { fromNewtype } from 'io-ts-types/lib/fromNewtype'
11 * import * as t from 'io-ts'
12 * import { right } from 'fp-ts/lib/Either'
13 * import { PathReporter } from 'io-ts/lib/PathReporter'
14 * import { Newtype, iso } from 'newtype-ts'
15 *
16 * interface Token extends Newtype<{ readonly Token: unique symbol }, string> {}
17 *
18 * const T = fromNewtype<Token>(t.string)
19 *
20 * assert.deepStrictEqual(T.decode('sometoken'), right(iso<Token>().wrap('sometoken')))
21 * assert.deepStrictEqual(PathReporter.report(T.decode(42)), ['Invalid value 42 supplied to : fromNewtype(string)'])
22 *
23 * @since 0.5.2
24 */
25export declare function fromNewtype<N extends AnyNewtype = never>(
26 codec: t.Type<CarrierOf<N>, t.OutputOf<CarrierOf<N>>>,
27 name?: string
28): t.Type<N, CarrierOf<N>, unknown>
29
\No newline at end of file