type Nullish = null | undefined; /** * If `T` is an array of functions, a union of the return type of those functions. */ type ReturnTypes) => any>> = T extends Array<(...a: Array) => infer R> ? R : never; /** * If `T` is a `Promise`, the type of the values to which the promise resolves. Otherwise `T` itself. */ type Resolve = T extends Promise ? A : T; /** * An intersection of the types of which the union `T` consists. */ type Intersect = (T extends any ? (faux: T) => void : never) extends (faux: infer U) => void ? U : never; /** * Like `Exclude` except that if `T` is a `Promise`, the exclusion logic is applied to the type of the values to which * the promise resolves instead of to `T` directly. */ type ExcludeAsynchronous = T extends Promise ? A extends U ? never : Promise : T extends U ? never : T; /** * Like `Extract` except that if `T` is a `Promise`, the extraction logic is applied to the type of the values to which * the promise resolves instead of to `T` directly. */ type ExtractAsynchronous = T extends Promise ? A extends U ? Promise : never : T extends U ? T : never; /** * If `T` is a `Promise` and `U` is not a `Promise`, a `Promise` which resolves to values of type `U`. Otherwise `U` * itself. */ type TransferAsynchronicity = T extends Promise ? T : U extends Promise ? Promise : T; /** * Calls the passed callback, forwarding the first argument and routing back whatever is returned. * * This is a simplified implementation of `run`: * ``` * function run(value, callback) { * return callback(value); * } * ``` * * #### Promises * * If the first argument is a promise, the value to which that promise resolves is forwarded to the passed callback * instead of the promise itself. As a result, the call to the passed callback is delayed until the promise resolves. * If the promise rejects, the passed callback is skipped. * * #### Chains * * If multiple callbacks are passed, they are called subsequently. `run(x, a, b)` is equivalent to `run(run(x, a), b)`. */ declare function run(this: C, value: T, callback: (this: C, value: Resolve) => R): TransferAsynchronicity; declare function run(this: C, value: T, ...callbacks: [(this: C, value: Resolve) => Z, (this: C, value: Resolve) => R]): TransferAsynchronicity; declare function run(this: C, value: T, ...callbacks: [(this: C, value: Resolve) => Z, (this: C, value: Resolve) => Y, (this: C, value: Resolve) => R]): TransferAsynchronicity; declare function run(this: C, value: T, ...callbacks: [(this: C, value: Resolve) => Z, (this: C, value: Resolve) => Y, (this: C, value: Resolve) => X, (this: C, value: Resolve) => R]): TransferAsynchronicity; declare function run(this: C, value: T, ...callbacks: [(this: C, value: Resolve) => Z, (this: C, value: Resolve) => Y, (this: C, value: Resolve) => X, (this: C, value: Resolve) => W, (this: C, value: Resolve) => R]): TransferAsynchronicity; /** * Calls the passed callback ‒ forwarding the argument and routing back whatever is returned ‒ if the first argument is * not null-ish. If the first argument is null-ish, it is returned directly and the passed callback is skipped. * * This is a simplified implementation of `runIf`: * ``` * function runIf(value, callback) { * return value != null ? callback(value) : value; * } * ``` * * #### Promises * * If the first argument is a promise, the value to which that promise resolves is forwarded to the passed callback * instead of the promise itself. As a result, the call to the passed callback is delayed until the promise resolves. * If the value to which the promise resolves is null-ish or the promise rejects, the passed callback is skipped. * * #### Chains * * If multiple callbacks are passed, they are called subsequently—respecting the logic regarding null-ish values. * `runIf(x, a, b)` is equivalent to `runIf(runIf(x, a), b)` */ declare function runIf(this: C, value: T, callback: (this: C, value: Exclude, Nullish>) => R): ExtractAsynchronous | TransferAsynchronicity, R>; declare function runIf(this: C, value: T, ...callback: [(this: C, value: Exclude, Nullish>) => Z, (this: C, value: Exclude, Nullish>) => R]): ExtractAsynchronous | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, R>; declare function runIf(this: C, value: T, ...callback: [(this: C, value: Exclude, Nullish>) => Z, (this: C, value: Exclude, Nullish>) => Y, (this: C, value: Exclude, Nullish>) => R]): ExtractAsynchronous | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, R>; declare function runIf(this: C, value: T, ...callback: [(this: C, value: Exclude, Nullish>) => Z, (this: C, value: Exclude, Nullish>) => Y, (this: C, value: Exclude, Nullish>) => X, (this: C, value: Exclude, Nullish>) => R]): ExtractAsynchronous | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, R>; declare function runIf(this: C, value: T, ...callback: [(this: C, value: Exclude, Nullish>) => Z, (this: C, value: Exclude, Nullish>) => Y, (this: C, value: Exclude, Nullish>) => X, (this: C, value: Exclude, Nullish>) => W, (this: C, value: Exclude, Nullish>) => R]): ExtractAsynchronous | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, ExtractAsynchronous> | TransferAsynchronicity, R>; /** * Calls the passed callback, forwarding the first argument and returning that argument afterwards. * * This is a simplified implementation of `apply`: * ``` * function apply(value, callback) { * callback(value); * return value; * } * ``` * #### Promises * * If the first argument is a promise, the value to which that promise resolves is forwarded to the passed callback * instead of the promise itself. As a result, the call to the passed callback is delayed until the promise resolves. * If the promise rejects, the passed callback is skipped. * * #### Chains * * If multiple callbacks are passed, they are called subsequently. `apply(x, a, b)` is equivalent to * `apply(apply(x, a), b)`. */ declare function apply(this: C, value: T, callback: (this: C, value: Resolve) => Z): TransferAsynchronicity; // ↑ This overload is not strictly necessary. The one below is a generalised form of it. declare function apply) => any>, C>(this: C, value: T, ...callbacks: U): TransferAsynchronicity>, T>; export { run, runIf, apply };