Skip to content

Bring typeof switch behaviour inline with if - #27182

Closed
Jack Williams (jack-williams) wants to merge 10 commits into
microsoft:masterfrom
jack-williams:correct-narrowing-for-typeof-unknown-in-switch
Closed

Bring typeof switch behaviour inline with if#27182
Jack Williams (jack-williams) wants to merge 10 commits into
microsoft:masterfrom
jack-williams:correct-narrowing-for-typeof-unknown-in-switch

Conversation

@jack-williams

@jack-williamsJack Williams (jack-williams) commented Sep 18, 2018

Copy link
Copy Markdown
Collaborator

This PR has now morphed into something that bundles a couples of changes to essentially bring switch narrowing inline with if, which was recently updated in two keys ways:

  1. unknown narrowing for "function" and "object"
  2. narrowing aswell as filtering for union types.

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

@jack-williamsJack Williams (jack-williams) changed the title Fix typeof switch narrowing for unknown and keyofFix typeof switch narrowing for unknownSep 18, 2018
@jack-williams

Copy link
Copy Markdown
CollaboratorAuthor

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.

@jack-williamsJack Williams (jack-williams) changed the title Fix typeof switch narrowing for unknownBring typeof switch behaviour inline with ifSep 25, 2018
@jack-williamsJack Williams (jack-williams) changed the title Bring typeof switch behaviour inline with ifBring typeof switch behaviour inline with ifSep 25, 2018
@jack-williams
Jack Williams (jack-williams)force-pushed the correct-narrowing-for-typeof-unknown-in-switch branch from b4344f7 to 88a4fdfCompareOctober 4, 2018 10:13
@justingrant

Justin Grant (justingrant) commented Oct 5, 2018

Copy link
Copy Markdown
Contributor

Jack Williams (@jack-williams)Ryan Cavanaugh (@RyanCavanaugh) - will this PR remove the compile error from the code below? Or will it only affect switch (typeof) and not other kinds of switch expressions? I assume the latter, but wanted to check before opening a dupe issue.

// compiled with strictNullChecksfunctionrepro(x: string[]|null){switch(x&&x.length){case1: console.log(x[0]);// compiler error: Object is possibly nullbreak;default: console.log('invalid x');break;}if(x&&x.length===1){console.log(x[0]);// no compiler error}else{console.log('invalid x');}}

@jack-williams

Copy link
Copy Markdown
CollaboratorAuthor

Justin Grant (@justingrant)

I assume the latter, but wanted to check before opening a dupe issue.

You're right, this PR will only affect switch (typeof x) I'm afraid.

@justingrant

Copy link
Copy Markdown
Contributor

OK thanks Jack Williams (@jack-williams). After some more searching, it looks like the problem I ran into was #24091.

@jack-williams

Copy link
Copy Markdown
CollaboratorAuthor

Ryan Cavanaugh (@RyanCavanaugh)

Closing in favour of #27680

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typeof switch filters union types but does not narrow them Switch on typeof does not narrow unknown correctly.

4 participants

@jack-williams@justingrant@weswigham@RyanCavanaugh