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

option overview

Added in v0.5.0


Table of contents


OptionC (interface)

Given a codec representing a type A, returns a codec representing Option<A> that is able to deserialize the JSON representation of an Option.

Signature

export interface OptionC<C extends t.Mixed> extends t.Type<Option<t.TypeOf<C>>, OptionOutput<t.OutputOf<C>>, unknown> {}

Example

import { option } from 'io-ts-types/lib/option'
import { right } from 'fp-ts/lib/Either'
import { none, some } from 'fp-ts/lib/Option'
import * as t from 'io-ts'
import { PathReporter } from 'io-ts/lib/PathReporter'

const T = option(t.number)

assert.deepStrictEqual(T.decode(none), right(none))
assert.deepStrictEqual(T.decode(some(1)), right(some(1)))
assert.deepStrictEqual(PathReporter.report(T.decode(some('a'))), [
  'Invalid value "a" supplied to : Option<number>/1: Some<number>/value: number'
])

Added in v0.5.0

NoneOutput (type alias)

Signature

export type NoneOutput = t.OutputOf<typeof None>

Added in v0.5.18

OptionOutput (type alias)

Signature

export type OptionOutput<A> = NoneOutput | SomeOutput<A>

Added in v0.5.18

SomeOutput (type alias)

Signature

export type SomeOutput<A> = { _tag: 'Some'; value: A }

Added in v0.5.18

option

Signature

export function option<C extends t.Mixed>(codec: C, name: string = `Option<${codec.name}>`): OptionC<C> { ... }

Added in v0.5.0