Why doesn't awaiting a Promise<never> change reachability?
See original GitHub issueTypeScript Version: 3.7.2
Search Terms:
- “Promise<never>”
await
- reachability analysis
- control flow analysis
- definite assignment analysis
Code
(async () => {
let b: string;
let a1 = await Promise.reject(); // returns Promise<never>
b = ""; // Not unreachable?
b.toUpperCase(); // Not unreachable?
})();
Expected behavior:
As a1
is inferred to be never
(e.g. behaves like in the non-promised version), I expected the rest of the code to be marked as unreachable aswell:
function returnNever(): never { throw new Error(); }
(async () => {
let b: string;
let a0 = returnNever(); // a0 is never
b = ""; // Unreachable
b.toUpperCase(); // Unreachable
})();
Actual behavior: The code after the never-returning promise is marked as reachable.
Related Question on StackOverflow: Has more code: https://stackoverflow.com/questions/58732814 Related Issue: https://github.com/microsoft/TypeScript/issues/10973 (although marked as “Working as intended”, it was changed later. 3.7.2 behaves like the issue opener expected).
If this is not a bug, what is the background for this behavior?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Why is Promise<never> not considered in reachability analysis?
It seems that there is a reason to not consider await s of Promise<never> -returning functions in any aspect of the control flow...
Read more >await - JavaScript - MDN Web Docs
If the value is not a Promise , await converts the value to a resolved Promise , and waits for it. The awaited...
Read more >javascript - await does not wait for Promise to finish
So it's a bit weird. The "waiting" happens within that aync function, not your initial js function. consider this example
Read more >Issues · microsoft/TypeScript · GitHub
String literal rename doesn't work in object unions · #41489 opened Nov 11, 2020 ; Why doesn't awaiting a Promise<never> change reachability? ·...
Read more >Asynchronous Programming - Eloquent JavaScript
But that doesn't help when we want a single program to be able to make progress while it is waiting for a network...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah, since we actually error on unreachable code, this does seem worthy of the bug label.
Consider a
logErrorAsync
function that returnsPromise<never>
. It awaits something and then throws an error on every path (or goes into an infinite loop etc.). If the function does not return aPromise<never>
but anever
instead, it behaves as expected.As of TS 3.7.3: