Typescript: Add `PromiseResult` type to standard lib

Created on 24 Oct 2018  ·  6Comments  ·  Source: microsoft/TypeScript

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:

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

Most helpful comment

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.

All 6 comments

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.

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

Any progress here? :)

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.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

weswigham picture weswigham  ·  3Comments

wmaurer picture wmaurer  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

CyrusNajmabadi picture CyrusNajmabadi  ·  3Comments

Antony-Jones picture Antony-Jones  ·  3Comments