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> |