Skip to content

withFallback ​

This function returns a new function that either returns the result of func or a fallback value defaultValue if the result is null or undefined.

Added in v0.0.2

Usage ​

ts
import { withFallback } from 'parsnip-kit'

const func = (a: number) => (a === 0 ? null : a)
const funcWithDefault = withFallback(func, -1)

funcWithDefault(1)
// 1
funcWithDefault(0)
// -1

API ​

Type Parameter ​

ArgTypeDescription
Textends (...args: any[]) => anyFunction type
RType of default value

Arguments ​

ArgTypeOptionalDefaultDescription
funcTfalseundefinedThe function with default return value
defaultValueRfalseundefinedDefault value

Returns ​

Type
WithFallback<T, R>

Reference ​

WithFallback