-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Inconsistent/broken narrowing of void #32809
Comments
The only behavior I would consider a bug here is the wrong error message in the first |
Isn't that precisely why narrowing should be a no-op? To be able to narrow out undefined and get to never (the I guess narrowing to |
I find that convincing. The problem with Probably the right answer is that in all cases, |
TypeScript Version: 3.5.1
Search Terms: void typeof undefined narrow use before assign
Code
Expected behavior:
Using
x2
inside the first conditional in thestrictNullChecks
example should not error, as the variable absolutely was assigned to before use (whatever its type should be).Also, the semantics around narrowing
void
should be consistent whenstrictNullChecks
is on and when it's off. And, based on some of the comments I've seen aboutvoid
's purpose, it seems like the semantics with strictNullChecks off are the right ones.Actual behavior:
The use of
x2
triggers an error, as mentioned.And narrowing
void
is a no-op whenstrictNullChecks
is disabled (i.e., it always results invoid
), whereas narrowingvoid
can producenever
whenstrictNullChecks
is enabled.Playground Link:
With strictNullChecks: https://www.typescriptlang.org/play/index.html#code/PTAEHcEsBcAtQM7QE6QMbQHIFcA2uBhWAUzQGsEAoNAewDslQAPAJgC5QA3GyAE1AC8obHV7EAZpDrFeAbmr1GTAMwdufQcNESpM+ZUjiAFNACeAB2I1xzFoIFCARCLGTpvRwEpQAb0qgA21AQUABRZGQaZA4ANQBDVDiAI1xiUAByVnTQSARhBBlQJIkotOKpAHNQOIQESAr3ADpKAF9KA2MzS2tmZVAAQgdQZ203GS9ff0CVYLBpTmJkVqA
Without strictNullChecks: https://www.typescriptlang.org/play/index.html?strictNullChecks=false#code/PTAEHcEsBcAsHsCu1QGdoCdIGNoDlEAbQgYVgFNsBrVAKG3gDt1QAPAJgC5QA3eSACagAvKESMB5AGaRG5AQG5akKQApoATwAO5eFLbsRw0QCJxkmXIEmAlKADetUM4OgQrjOQC2AQ1mpefiEKTwAaUEZ4FHNpWXkAOloAX1plNU0dPVcAQmNQMwlYqxNQADJSnLzGIkI7RxdXdw5QT19-QMFQEPJwjB84cgxQOB9GCPIeQcSkoA
The text was updated successfully, but these errors were encountered: