Skip to content

throwIf() function

throw return of err if predicate function return true

default throw function is identity

Signature:

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

Example

ts
 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

Released under the Apache-2.0 License.