withFallback
この関数は、func の結果を返すか、結果が null または undefined の場合にデフォルト値 defaultValue を返す新しい関数を返します。
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)
// -1API
Type Parameter
| Arg | Type | Description |
|---|---|---|
T | extends (...args: any[]) => any | 関数型 |
R | | デフォルト値の型 |
Arguments
| Arg | Type | Optional | Default | Description |
|---|---|---|---|---|
func | T | false | undefined | デフォルトの戻り値を持つ関数 |
defaultValue | R | false | undefined | デフォルト値 |
Returns
| Type |
|---|
WithFallback<T, R> |