Skip to main content

throwIf

throwIf() function

throw return of err if predicate function return true

default throw function is identity

Signature:
declare function throwIf<T>(predicate: (input: T) => boolean, err?: (input: T) => unknown): (input: T) => T;

declare function throwIf<T, N extends T>(predicate: (input: T) => input is N, err?: (input: N) => unknown): (input: T) => Exclude<T, N>;

Example

 pipe(
fn(), // return type is string | undefined

throwIf(isUndefined, (err) => Error("return of fn() is undefined")),
// err is undefined, and it is return of fn

(input) => input, // input is string
)

Open Source Code