UNPKG

1.41 kBTypeScriptView Raw
1/**
2 * @since 0.5.0
3 */
4import { Option } from 'fp-ts/lib/Option'
5import * as t from 'io-ts'
6declare const None: t.ExactC<
7 t.TypeC<{
8 _tag: t.LiteralC<'None'>
9 }>
10>
11/**
12 * @since 0.5.18
13 */
14export declare type NoneOutput = t.OutputOf<typeof None>
15/**
16 * @since 0.5.18
17 */
18export declare type SomeOutput<A> = {
19 _tag: 'Some'
20 value: A
21}
22/**
23 * @since 0.5.18
24 */
25export declare type OptionOutput<A> = NoneOutput | SomeOutput<A>
26/**
27 * Given a codec representing a type `A`, returns a codec representing `Option<A>` that is able to deserialize
28 * the JSON representation of an `Option`.
29 *
30 * @example
31 * import { option } from 'io-ts-types/lib/option'
32 * import { right } from 'fp-ts/lib/Either'
33 * import { none, some } from 'fp-ts/lib/Option'
34 * import * as t from 'io-ts'
35 * import { PathReporter } from 'io-ts/lib/PathReporter'
36 *
37 * const T = option(t.number)
38 *
39 * assert.deepStrictEqual(T.decode(none), right(none))
40 * assert.deepStrictEqual(T.decode(some(1)), right(some(1)))
41 * assert.deepStrictEqual(PathReporter.report(T.decode(some('a'))), ['Invalid value "a" supplied to : Option<number>/1: Some<number>/value: number'])
42 *
43 * @since 0.5.0
44 */
45export interface OptionC<C extends t.Mixed> extends t.Type<Option<t.TypeOf<C>>, OptionOutput<t.OutputOf<C>>, unknown> {}
46/**
47 * @since 0.5.0
48 */
49export declare function option<C extends t.Mixed>(codec: C, name?: string): OptionC<C>
50export {}