Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: AsyncReturnType #49

Closed
dylang opened this issue Jul 17, 2019 · 10 comments · Fixed by #78
Closed

Proposal: AsyncReturnType #49

dylang opened this issue Jul 17, 2019 · 10 comments · Fixed by #78
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@dylang
Copy link

dylang commented Jul 17, 2019

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.

@BendingBender
Copy link
Contributor

What's the practical difference to #42 (apart from the fact that ResolvedType is a combination of ReturnType and the proposed Unpromise)?

@sindresorhus
Copy link
Owner

@dylang ⬆️

@fregante
Copy link
Collaborator

fregante commented Sep 17, 2019

What's the practical difference to #42

I think that's difference. It's just a combination of them:

Unpromise<ReturnType<typeof asyncFunction>>
ResolvedType<typeof asyncFunction>

Funny thing is that all the examples in #38 and #42 are exactly:

Unpromise<ReturnType<typeof asyncFunction>>

So perhaps ResolvedType makes more sense than a standalone Unpromise.


Also maybe it should be called AsyncReturnType instead of ResolvedType

@fregante
Copy link
Collaborator

This is what I'm using currently:

type Unpromise<MaybePromise> = MaybePromise extends Promise<infer Type> ? Type : MaybePromise;
type AsyncReturnType<T extends (...args: any) => any> = Unpromise<ReturnType<T>>

You can probably merge the two and avoid Unpromise but I don't know how 😳

@sindresorhus
Copy link
Owner

I still think Unpromise makes sense as an independent building block.

PR welcome for AsyncReturnType.

@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels Nov 29, 2019
@sindresorhus sindresorhus changed the title Proposal: ResolvedTyped - ReturnType for promises Proposal: AsyncReturnType Nov 29, 2019
@sindresorhus
Copy link
Owner

Relevant TypeScript issue: microsoft/TypeScript#28105

@fregante
Copy link
Collaborator

@sindresorhus I think that's more about Unpromise and AsyncReturnType

A PR for that issue has been opened: microsoft/TypeScript#35998

@resynth1943
Copy link
Contributor

resynth1943 commented Feb 1, 2020

PR welcome for AsyncReturnType.

@sindresorhus Since you're accepting PRs for this, do you want me to submit one for this after PromiseValue has been merged?

@sindresorhus
Copy link
Owner

@resynth1943 Yes. That would be great. 🙌

@resynth1943
Copy link
Contributor

Gotcha. When you get a chance, @sindresorhus, could you check out #75?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants