-
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
Narrowing on await #44858
Comments
Even synchronous code can still produce the error. let x: null | string = 'hello';
(() => {
if (typeof x !== 'string') {
return;
}
function fn() {
x = null;
}
fn();
// Cannot read property 'toString' of null
console.log(x.toString());
})(); Because TS can't analyse every function to check if they change anything, so it treats every function as having no side-effect. |
Actually TS can analyze every function, by |
It is still in discussion. I guess you might be interested in #7770. |
@whzx5byb Thank you! I would have spent quite some time looking for these links. |
What a nice joke! Thank you! |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
narrowing await
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Type System Behavior
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
x
is narrowed tostring
, butawait
should have either extended it back tonull | string
, or narrowed it in the parameter ofPromise
constructor.toString
gets called onnull
. In this way arbitrary type punning is possible.π Expected behavior
Code shouldn't have compiled.
The text was updated successfully, but these errors were encountered: