Skip to content
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

Conversation

jack-williams
Copy link
Collaborator

@jack-williams jack-williams commented Sep 18, 2018

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-williams jack-williams changed the title Fix typeof switch narrowing for unknown and keyof Fix typeof switch narrowing for unknown Sep 18, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Sep 18, 2018
@jack-williams
Copy link
Collaborator Author

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-williams jack-williams changed the title Fix typeof switch narrowing for unknown Bring typeof switch behaviour inline with if Sep 25, 2018
@jack-williams jack-williams changed the title Bring typeof switch behaviour inline with if Bring typeof switch behaviour inline with if Sep 25, 2018
@jack-williams jack-williams force-pushed the correct-narrowing-for-typeof-unknown-in-switch branch from b4344f7 to 88a4fdf Compare October 4, 2018 10:13
@justingrant
Copy link
Contributor

justingrant commented Oct 5, 2018

@jack-williams @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 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'); 
  }

}

@jack-williams
Copy link
Collaborator Author

@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
Contributor

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

@jack-williams
Copy link
Collaborator Author

@RyanCavanaugh

Closing in favour of #27680

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants