-
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
Bring typeof switch behaviour inline with if #27182
Bring typeof switch behaviour inline with if #27182
Conversation
I'm tempted to re-open this PR with a fresh one to tidy up all the history into a single commit. Just give me a thumbs up / response and I'll sort it out, link to the new one, and close this. |
typeof
switch behaviour inline with if
typeof
switch behaviour inline with if
b4344f7
to
88a4fdf
Compare
@jack-williams @RyanCavanaugh - will this PR remove the compile error from the code below? Or will it only affect // compiled with strictNullChecks
function repro (x: string[] | null) {
switch (x && x.length) {
case 1:
console.log (x[0]); // compiler error: Object is possibly null
break;
default:
console.log ('invalid x');
break;
}
if (x && x.length===1) {
console.log (x[0]); // no compiler error
} else {
console.log ('invalid x');
}
} |
You're right, this PR will only affect |
OK thanks @jack-williams. After some more searching, it looks like the problem I ran into was #24091. |
Closing in favour of #27680 |
This PR has now morphed into something that bundles a couples of changes to essentially bring
switch
narrowing inline withif
, which was recently updated in two keys ways:unknown
narrowing for"function"
and"object"
I don't know if this PR should be split in separate requests tackling different issues. This can probably be resolved after the release of 3.1 when 3.2 is more relevant.
Fixes #27180
Fixes #27335
Adds tests for #27181