-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow the use of the async keyword in types to replace Promise #31709
Comments
I think this would be really useful and align with the idiomatic JS quite nicely. |
Came across a use-case today where I allowed this: I updated the issue with my thoughts on the topic, what do you think?. |
With your changes, this becomes related to #31394 |
True, but I do think it would be better to use the more strict approach here and infer only |
Again, this is might not as straightforward as it seems. While in purely in terms of types this is rather simple, the actual definition would still be defined as a Current: const foo: <T>(t: T) => Promise<T> = async <T>(t: T): Promise<T> => {
return {} as T;
}; The issue with the idea: const bar: async <T>(t: T) => T = async <T>(t: T): Promise<T> => {
return {} as T;
}; Only changing the types behavior would lead to some inconsistencies where in the type, async would mean that the return type is wrapped in a Promise while in the actual function definition it's not. This would be really confusing. For this I think this is not something that would be a welcome change in TypeScript, not even a good one. I know I went a complete 180° here, but I think TypeScript is a great tool for JavaScript and as such it shouldn't mask the actual behavior of it under inconsistent keywords. I'm leaving this up so someone from the TypeScript team might see this and give a proper opinion about the topic. I might be wrong in the proposal, I might be wrong here. |
Because 1) Types are erased at compile time, 2) The proposed |
We went through the same line of thinking when designing the type system functionality for |
Search Terms
allow alias async return type promise
Suggestion
Right now if I specify the type of an async function I write
foo: () => Promise<string>
because that's what it is, fair enough. But it's kind of ugly. The point of async/await is to hide all the ugliness of Promises under syntactic sugar.TypeScript should do the same and hide the type of the
Promise
by providing an alternative declaration. Allow us to use theasync
keyword in type contexts instead of usingPromise<?>
So this:
could mean the same thing as this:
And would still provide the same IntelliSense as
Promise<T>
I've taken into consideration whether it should also allow for
T
too:My reasoning is that in plain JavaScript awaiting something that's not a
Promise
just returns that value and TypeScript should respect this.This however would completely cripple IntelliSense.
But if it's the case for you, you can always fall back to the
<T>() => Promise<T> | T
type annotation. (Anasync?
keyword wouldn't really be idiomatic)Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: