Skip to content

Add PromiseResult type to standard lib #28105

Closed
@ethanresnick

Description

@ethanresnick
Contributor

Search Terms

promise result type

Suggestion

Typescript already ships with a ReturnType type and a Parameters type, so why not ship with a PromiseResult type:

type PromiseResult<T> = T extends Promise<infer U> ? U : never;

Use Cases/Examples

type Output = PromiseResult<ReturnType<typeof computeSomething>>;

async function computeSomething(input: any) {}
function computeFromResult(input: Output) {}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
    This wouldn't change the runtime behavior of existing JavaScript code
    This could be implemented without emitting different JS based on the types of the expressions
    This isn't a runtime feature (e.g. new expression-level syntax)

Activity

DanielRosenwasser

DanielRosenwasser commented on Oct 24, 2018

@DanielRosenwasser
Member

See #17077, and discussion at

Search also for awaited, await, promised, unpromise, and unpromisify on the issue tracker.

DanielRosenwasser

DanielRosenwasser commented on Oct 24, 2018

@DanielRosenwasser
Member

Just to clarify, for Promises to work correctly we need a more general awaited type which unwraps PromiseLikes recursively. If we had that, it would subsume this PromiseResult type.

ajafff

ajafff commented on Oct 24, 2018

@ajafff
Contributor

Just to clarify, for Promises to work correctly we need a more general awaited type which unwraps PromiseLikes recursively.

Sounds like recursive conditional types to me... but that's already discussed somewhere else

deser

deser commented on Dec 12, 2019

@deser

Any progress here? :)

westonkjones

westonkjones commented on Dec 19, 2019

@westonkjones

Interested in this as well. The AWS SDK uses this as the return of their promises
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SecretsManager.html#getSecretValue-property

(method) Request<SecretsManager.GetSecretValueResponse, AWSError>.promise(): Promise<PromiseResult<AWS.SecretsManager.GetSecretValueResponse, AWS.AWSError>>

Returns a 'thenable' promise.
fregante

fregante commented on Jan 11, 2020

@fregante

Coming in #35998, hopefully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ethanresnick@DanielRosenwasser@fregante@deser@westonkjones

        Issue actions

          Add `PromiseResult` type to standard lib · Issue #28105 · microsoft/TypeScript