Skip to content

throwIf() function

如果谓词函数返回 true,则抛出 err 的返回值。

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.