Skip to content

unless() function

If the result of predicate is true, process will not be executed. unless return the result of process if it is executed, and if the process is not executed, it returns the function argument as is.

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.