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

withFallback overview

Added in v0.5.0

Table of contents


Returns a clone of the given codec that always succeed using the given value a if the original codec fails


export function withFallback<C extends t.Any>(codec: C, a: t.TypeOf<C>, name = `withFallback(${})`): C { ... }


import { withFallback } from 'io-ts-types/lib/withFallback'
import * as t from 'io-ts'
import { right } from 'fp-ts/lib/Either'

const T = withFallback(t.number, -1)

assert.deepStrictEqual(T.decode(1), right(1))
assert.deepStrictEqual(T.decode('a'), right(-1))

Added in v0.5.0