-
-
Notifications
You must be signed in to change notification settings - Fork 616
Closed
Labels
enhancementNew feature or requestNew feature or requesthelp wantedExtra attention is neededExtra attention is needed
Description
Reason
TypeScript provides ReturnType to infer what type a function returns, but it isn't as as useful when functions return a promise and you want to know what type the promise resolves to.
Proposal
ResolvedType
which returns the type of a function that returns a promise, but also works if the function is not a promise.
Example
// Silly simple example
const asyncFunction = async (n: number) => n * 2;
// We have this today
type AsyncFunctionReturns = ReturnType<typeof asyncFunction>;
// => Promise<number>
// This is the proposal
type AsyncFunctionReturns = ResolvedType<typeof asyncFunction>;
// => number
Proof of concept
Not tested extensively, may have bugs!
// For any function, return the returnType or promiseResolved type.
export type ResolvedValue<FuncPromOrValue> = FuncPromOrValue extends (...args: any) => any
? ReturnType<FuncPromOrValue> extends Promise<infer Result> // Is a function.
? Result // Is a promise, return result from Promise.
: ReturnType<FuncPromOrValue> // Non-promise function, return result.
: FuncPromOrValue; // Not a promise or function, return type.
Existing work
I haven't seen anything like this before, but that doesn't mean it doesn't exist. I'm not fluent with other typed languages to know if there's a reason this doesn't exist or better way to implement it.
chetansastry, colinking, bartlangelaan and jukbot
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requesthelp wantedExtra attention is neededExtra attention is needed