1 | import type {IsUnknown} from './is-unknown';
|
2 |
|
3 | /**
|
4 | Create a function type with a return type of your choice and the same parameters as the given function type.
|
5 |
|
6 | Use-case: You want to define a wrapped function that returns something different while receiving the same parameters. For example, you might want to wrap a function that can throw an error into one that will return `undefined` instead.
|
7 |
|
8 | @example
|
9 | ```
|
10 | import type {SetReturnType} from 'type-fest';
|
11 |
|
12 | type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;
|
13 |
|
14 | type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;
|
15 | //=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;
|
16 | ```
|
17 |
|
18 | @category Function
|
19 | */
|
20 | export type SetReturnType<Function_ extends (...arguments_: any[]) => any, TypeToReturn> =
|
21 | // Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
|
22 | Function_ extends (this: infer ThisArgument, ...arguments_: infer Arguments) => any ? (
|
23 | // If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
|
24 | // We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
|
25 | IsUnknown<ThisArgument> extends true ? (...arguments_: Arguments) => TypeToReturn : (this: ThisArgument, ...arguments_: Arguments) => TypeToReturn
|
26 | ) : (
|
27 | // This part should be unreachable, but we make it meaningful just in case…
|
28 | (...arguments_: Parameters<Function_>) => TypeToReturn
|
29 | );
|