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.