microsoft / microsoft/TypeScript
async function drops `Awaited<Type>`
オープン
@rbuckton がすでに取り組んでいます。
2023年10月12日 から。
Needs Investigation
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
async function drops Awaited
Awaited swallowed in async
Awaited removed
🕗 Version & Regression Information
- This is the behavior in every TS version I tried, started occurring since TS v 4.5.5 when the
Awaitedwas introduced.
⏯ Playground Link
💻 Code
// signatures of the functions are copied from Promise.race
function race_identity_ok<T extends readonly unknown[] | []>( values: T): Promise<Awaited<T[number]>> {
return Promise.race(values) // OK, identity function works just fine
}
function race_wrapped_in_async_not_ok<T extends readonly unknown[] | []>( values: T): Promise<Awaited<T[number]>> {
const asyncWrapper = async () => {
const race = await Promise.race(values)
// const race: Awaited<T[number]> OK, this type is expected
return race
}
// const asyncWrapper: () => Promise<T[number]> this is not OK,
// we expect type () => Promise<Awaited<T[number]>>, but the `Awaited<>` was dropped, perhaps?
return asyncWrapper()
// error:
// Type 'Promise<T[number]>' is not assignable to type 'Promise<Awaited<T[number]>>'.
// Type 'T[number]' is not assignable to type 'Awaited<T[number]>'.(2322)
}```
### 🙁 Actual behavior
The `Awaited<Type>` is dropped from the return type of the function `asyncWrapper`, but it should not be (at least as I can tell).
### 🙂 Expected behavior
The `Awaited` should not be dropped.
I believe so because I assume the `Awaited` in the `Promise.race` signature is of crucial importance, as it denotes that if type `T` is a thenable (or `PromiseLike`, if you like), it is unwrapped in the return type.
Wrapping the `Promise.race` in `async` function (`asyncWrapper`) looses the unwrapping semantics, in my opinion.
### Additional information about the issue
The issue might seem artificial, however, this is the minimalist example I came up with. In our codebase, the issue is convoluted because we replace `PromiseLike` with [CancellablePromise](https://github.com/srmagura/real-cancellable-promise). We need the async function wrapper to correctly deduce return type (and keep the `Awaited`), so that the return type of `Promise.race` is perserved when fed through the [buildCancellablePromise](https://github.com/srmagura/real-cancellable-promise#combine-multiple-api-calls-into-a-single-async-flow).
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
評価
この issue はまだ評価されていません。