-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add a new type Awaitable<T> = T | PromiseLike<T>
#31394
Comments
At first I didn't see the utility of this since everything in JavaScript is awaitable, but I just realized it's good for type inference: type Awaitable<T> = T | PromiseLike<T>;
declare function foo<T>(callback: () => T): Promise<T>;
declare function bar<T>(callback: () => Awaitable<T>): Promise<T>;
const fun = () => Promise.resolve(812);
let food = foo(fun); // Promise<Promise<number>>, which is not actually a thing.
let bard = bar(fun); // Promise<number> - yes! |
I think Since any value is awaitable in TS this name seems too generic to me, |
I think Promisable and Awaitable are both fair, but I notice that ForAwaitable is another type under discussion and recommend that these two types are aligned regardless of the direction we go. |
Some feedback from future self: I have been using |
Control plugin's lifecycle via PluginSetup/PluginDispose Register plugin event and search engines via PluginContext TODO: implement plugin searcher/loader for external plugins TODO: move internal plugins to external packages
This was added in #45350 and will be part of TypeScript 4.5. |
I misinterpreted it. It’s not the same. |
type Awaitable<T> = T | PromiseLike<T>
I've been using this exact definition in many of my projects for years. It will be great if this is built in. |
Been using the same |
Is this relevant anymore given #51841 (comment)? |
I like |
Another name idea is |
This issue predates that comment and its references. I still think a builtin |
I know this is closed, but I want to highlight one important argument for this type that hasn't been raised. The type NumberCallback = () => Awaitable<number>;
// these both work!
const x: NumberCallback = () => 42;
const y: NumberCallback = async () => 42; |
The merits of an individual utility type aren't really relevant since our position is "No new utility types, at all". If the type is useful for you, you should definitely write it in your project with the definition you'd like it to have. |
@RyanCavanaugh thanks for the update - |
@btoo Not only that there's no such a record, they literally just added a new |
@MuTsunTsai i wouldn't call NoInfer a utility type as you cannot implement it yourself in library code. |
@ritschwumm Your opinion is irrelevant. They literally called it "The NoInfer Utility Type". |
Search Terms
awaitable
Suggestion
Add an awaitable type for values that will be awaited.
This is based on my question and a comment on StackOverflow
Use Cases
Two use cases come in mind immediately:
Promise.then()
/Promise.catch
/Promise.finally
callbacks.Also, this type could replace all 1334 occurrences that come up when running
git grep '| Promise'
in the current TypeScript code base.Examples
Callback example:
Promise example:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: