Skip to content

unless() function

predicate의 결과가 true이면 process가 실행되지 않습니다. unless는 실행되면 process의 결과를 반환하고, process가 실행되지 않으면 함수 인자를 그대로 반환합니다.

Signature:

typescript
declare function unless<T, N extends T, U>(
  predicate: (input: T) => input is N,
  process: (input: Exclude<T, N>) => U,
): (input: T) => N | (U extends void ? undefined : U);

Example

ts
// it will return only string
const unlessIsString: (input: string | undefined) => string = unless(
  isString,
  (input) => {
    throw Error("input is undefiend.");
  },
);

Open Source Code

Released under the Apache-2.0 License.