-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Description
🔎 Search Terms
"type narrowing", "async", "await", "promise"
🕗 Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about all relevant to async/await and
Promises - I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
async function test(): Promise<void> {
let firstResolvedPromiseType: "1" | "2" | undefined = undefined
const [promise1PromiseSettledResult, promise2PromiseSettledResult] = await Promise.allSettled([
Promise.resolve("SomePromise1").then(it => {
firstResolvedPromiseType ??= "1"
return it
}),
Promise.resolve("SomePromise2").then(it => {
firstResolvedPromiseType ??= "2"
return it
})
])
// The problem is that compiler sure the type is only undefined which is wrong
console.log(`firstResolvedPromiseType=${firstResolvedPromiseType}`)
console.log(promise1PromiseSettledResult)
console.log(promise2PromiseSettledResult)
}
test()🙁 Actual behavior
Compiler is absolutely sure that the only possible value of firstResolvedPromiseType is undefined which is wrong:
🙂 Expected behavior
Compiler correctly determine the type of firstResolvedPromiseType as "1" | "2" | undefined
Additional information about the issue
No response
Metadata
Metadata
Assignees
Labels
No labels