UNPKG

1.46 kBTypeScriptView Raw
1/**
2 * @since 0.5.0
3 */
4import { Either } from 'fp-ts/lib/Either'
5import * as t from 'io-ts'
6/**
7 * @since 0.5.18
8 */
9export declare type LeftOutput<L> = {
10 _tag: 'Left'
11 left: L
12}
13/**
14 * @since 0.5.18
15 */
16export declare type RightOutput<R> = {
17 _tag: 'Right'
18 right: R
19}
20/**
21 * @since 0.5.18
22 */
23export declare type EitherOutput<L, R> = LeftOutput<L> | RightOutput<R>
24/**
25 * @since 0.5.0
26 */
27export interface EitherC<L extends t.Mixed, R extends t.Mixed>
28 extends t.Type<Either<t.TypeOf<L>, t.TypeOf<R>>, EitherOutput<t.OutputOf<L>, t.OutputOf<R>>, unknown> {}
29/**
30 * Given a codec representing a type `L` and a codec representing a type `A`, returns a codec representing `Either<L, A>` that is able to deserialize
31 * the JSON representation of an `Either`.
32 *
33 * @example
34 * import { either } from 'io-ts-types/lib/either'
35 * import { left, right } from 'fp-ts/lib/Either'
36 * import * as t from 'io-ts'
37 * import { PathReporter } from 'io-ts/lib/PathReporter'
38 *
39 * const T = either(t.string, t.number)
40 *
41 * assert.deepStrictEqual(T.decode(right(1)), right(right(1)))
42 * assert.deepStrictEqual(T.decode(left('a')), right(left('a')))
43 * assert.deepStrictEqual(PathReporter.report(T.decode(right('a'))), ['Invalid value "a" supplied to : Either<string, number>/1: Right<string>/right: number'])
44 *
45 * @since 0.5.0
46 */
47export declare function either<L extends t.Mixed, R extends t.Mixed>(
48 leftCodec: L,
49 rightCodec: R,
50 name?: string
51): EitherC<L, R>
52
\No newline at end of file