Skip to content

unless() function

如果 predicate 的结果为 true,则不执行 process。如果执行了 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.