Description
Suggestion
Allow typing async function without generic Promise type.
π Search Terms
async, promise, return type
β Viability 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Async functions always return Promise, so there is no need to specificly write return type of that functions as Promise<type>
.
Combination of async function and simple return type should be enough for typesystem to handle that.
Instead:
async fn(): Promise<number[]>
This:
async fn(): number[]; // type system infers return type as Promise<number[]> automaticaly.
Currently results in TS error:
The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?ts(1064)
So TS clearly knows it MUST be Promise, but requires redundant annotation to prove it.
π» Use Cases
This is particulary useful for projects, which enforces writing down function return types (Angular, ...)
Writing Promise<X>
is already redundant as just second before you wrote async
while defining function.
Also converting function to async function requires changing return type.
Should be backwards compatible.
Overall my view of typesystem is to add benefit to developer with minimal action. Because of that, we have very good type infering.
This seems to me like handy addition.
And I'm not alone #40425 (comment)
In my head, this doesn't sound too complicated to implement π