Skip to content

throwIf() function

조건자 함수가 true를 반환하면 err의 반환값을 throw합니다.

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.