You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a new type Awaitable<T> = T | PromiseLike<T> #31394
Promise.resolve('Hello, world!').then(// This type annotation is silly. This is really just to show promise callbacks should accept `Awaitable<T>`.(hello): Awaitable<string>=>{console.log(hello);returnhello;},);
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, etc.)
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:
typeAwaitable<T>=T|PromiseLike<T>;declarefunctionfoo<T>(callback: ()=>T): Promise<T>;declarefunctionbar<T>(callback: ()=>Awaitable<T>): Promise<T>;constfun=()=>Promise.resolve(812);letfood=foo(fun);// Promise<Promise<number>>, which is not actually a thing.letbard=bar(fun);// Promise<number> - yes!
I think Promisable would be a better name than Awaitable.
Since any value is awaitable in TS this name seems too generic to me, Promisable on the other hand says to me that the value could be actually a Promise (technically a PromiseLike, but PromiseLikeable or PromiseableLike sound awful to me).
Reacted by Sindre Sorhus, Bernard, ExE Boss, Jonah Snider, Vishtar and Michael McGlynn
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.
Been using the same Promisable impl from #31394 (comment) since summertime last year. Hoping the same (it becoming a built-in)! It cuts down on a lot of code and improves the developer experience IMO.
I like Awaitable more than Promisable. The whole point of the type is that you can use await on the type to "unwrap" its generic type. It is a corollary to the Awaited type. Promisable to me conveys a semantic like PromiseLike which is not right.
This issue predates that comment and its references. I still think a builtin Awaitable type would be useful, but the TypeScript team is free to close this issue if they feel otherwise.
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.
Reacted by Cameron Steffen, Mu-Tsun Tsai, Kevin Marnholz and silverwind
Ryan Cavanaugh (@RyanCavanaugh) thanks for the update - just for my own learning, is there a decision record rationalizing the "No new utility types, at all" position I can read up on? nevermind, I found it
Activity
fatcerberus commentedon Jun 2, 2019
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:
fabiospampinato commentedon Jul 2, 2019
I think
Promisablewould be a better name thanAwaitable.Since any value is awaitable in TS this name seems too generic to me,
Promisableon the other hand says to me that the value could be actually a Promise (technically aPromiseLike, butPromiseLikeableorPromiseableLikesound awful to me).Vandivier commentedon Apr 4, 2020
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.
#36153
remcohaszing commentedon Apr 6, 2020
Some feedback from future self: I have been using
Promisablea lot. I really hope this will be a builtin type.refactor(plugin): Remake plugin-related interfaces
remcohaszing commentedon Nov 3, 2021
This was added in #45350 and will be part of TypeScript 4.5.
remcohaszing commentedon Nov 3, 2021
I misinterpreted it. It’s not the same.
[-]Add a new type Awaitable[/-][+]Add a new `type Awaitable<T> = T | PromiseLike<T>`[/+]MuTsunTsai commentedon Nov 21, 2022
I've been using this exact definition in many of my projects for years. It will be great if this is built in.
crookse commentedon May 31, 2023
Been using the same
Promisableimpl from #31394 (comment) since summertime last year. Hoping the same (it becoming a built-in)! It cuts down on a lot of code and improves the developer experience IMO.olivermrose commentedon May 31, 2023
Is this relevant anymore given #51841 (comment)?
camsteffen commentedon Mar 5, 2024
I like
Awaitablemore thanPromisable. The whole point of the type is that you can useawaiton the type to "unwrap" its generic type. It is a corollary to theAwaitedtype.Promisableto me conveys a semantic likePromiseLikewhich is not right.camsteffen commentedon Mar 5, 2024
Another name idea is
PromiseOr<T>. The Dart language has the same concept but calledFutureOr.remcohaszing commentedon Mar 5, 2024
This issue predates that comment and its references. I still think a builtin
Awaitabletype would be useful, but the TypeScript team is free to close this issue if they feel otherwise.camsteffen commentedon Mar 6, 2024
I know this is closed, but I want to highlight one important argument for this type that hasn't been raised.
The
Awaitabletype allows you to define function type that is "optionallyasync". Optionally async functions is a very widely useful pattern.RyanCavanaugh commentedon Mar 6, 2024
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.
btoo commentedon Mar 27, 2024
Ryan Cavanaugh (@RyanCavanaugh) thanks for the update -
just for my own learning, is there a decision record rationalizing the "No new utility types, at all" position I can read up on?nevermind, I found itMuTsunTsai commentedon Mar 29, 2024
btoo Not only that there's no such a record, they literally just added a new
NoInferutility type in v5.4 so I have no idea what he's talking about.ritschwumm commentedon Mar 29, 2024
Mu-Tsun Tsai (@MuTsunTsai) i wouldn't call NoInfer a utility type as you cannot implement it yourself in library code.
MuTsunTsai commentedon Mar 29, 2024
ritschwumm Your opinion is irrelevant. They literally called it "The NoInfer Utility Type".