Sha256: 60f476f1c4de44a08d6a566c6f1e1b7de6cbe53d9153c9cc2284ca0022e21fba

Contents?: true

Size: 1.68 KB

Versions: 33

Compression:

Stored size: 1.68 KB

Contents

type IsAny<T> = 0 extends (1 & T) ? true : false; // https://stackoverflow.com/a/49928360/3406963
type IsNever<T> = [T] extends [never] ? true : false;
type IsUnknown<T> = IsNever<T> extends false ? T extends unknown ? unknown extends T ? IsAny<T> extends false ? true : false : false : false : false;

/**
Create a function type with a return type of your choice and the same parameters as the given function type.

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.

@example
```
import type {SetReturnType} from 'type-fest';

type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;

type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;
//=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;
```

@category Function
*/
export type SetReturnType<Fn extends (...args: any[]) => any, TypeToReturn> =
	// Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
	Fn extends (this: infer ThisArg, ...args: infer Arguments) => any ? (
		// If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
		// We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
		IsUnknown<ThisArg> extends true ? (...args: Arguments) => TypeToReturn : (this: ThisArg, ...args: Arguments) => TypeToReturn
	) : (
		// This part should be unreachable, but we make it meaningful just in caseā€¦
		(...args: Parameters<Fn>) => TypeToReturn
	);

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.59 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.58 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.57 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.56 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.55 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.54 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.53 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.52 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.51 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.50 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.49 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.48 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.47 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.46 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.45 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.44 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.43 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.42 node_modules/type-fest/source/set-return-type.d.ts
immosquare-cleaner-0.1.41 node_modules/type-fest/source/set-return-type.d.ts