Skip to main content
Module

x/scaffold/src/deps/types.ts>SetReturnType

scaffold your next project with style and 💗
Latest
type alias SetReturnType
import { type SetReturnType } from "https://deno.land/x/scaffold@0.3.0/src/deps/types.ts";

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.

Examples

Example 1

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;

Type Parameters

Fn extends (...args: any[]) => any
TypeToReturn
definition: Fn extends (this: infer ThisArg, ...args: infer Arguments) => any ? (IsUnknown<ThisArg> extends true ? (...args: Arguments) => TypeToReturn : (this: ThisArg, ...args: Arguments) => TypeToReturn) : ((...args: Parameters<Fn>) => TypeToReturn)