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

Multiple cases inside of a switch statement not narrowing the type #43458

Open
boblauer opened this issue Mar 31, 2021 · 3 comments
Open

Multiple cases inside of a switch statement not narrowing the type #43458

boblauer opened this issue Mar 31, 2021 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@boblauer
Copy link

Bug Report

If you have a switch statement that has multiple cases that fall through, and those case statements are not first, TypeScript will not narrow your types in those statements.

It's a bit confusing to explain, but if you look at the playground link below, hopefully it makes sense. You can see if that you get rid of the first case statement, everything works just fine.

🔎 Search Terms

switch, case

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about switch statements

⏯ Playground Link

Playground link with relevant code

💻 Code

type Thing =
  | {
      type: 'a';
      onlyA: string;
    }
  | {
      type: 'b' | 'B';
      onlyB: string;
    };

const thing: Thing = {} as Thing;

switch (true) {
  case thing.type === 'B':
    break;

  case thing.type === 'b' && thing.onlyB === 'a': // TS error on this line
  case thing.type === 'a' && thing.onlyA === 'b': // and on this line
  case thing.type === 'a' && thing.onlyA === 'c':
    break;

  default:
    break;
}

🙁 Actual behavior

In the first 2 of the 3 case statements that are grouped together, TS will complain that onlyB and onlyA does not exist on type Thing. If you get rid of the very first case statement in the switch, the errors go away.

🙂 Expected behavior

I would expect this code to work as-is, without the need for the 3 case statements to be listed first in the switch statement.

@whzx5byb
Copy link

Related #37178 #8934

@boblauer
Copy link
Author

The one big difference between those issues and this one (as far as I can tell) is that in my example, the 3 grouped case statements do work if they're the first case statements in the switch:

https://www.typescriptlang.org/play?ts=4.3.0-dev.20210331#code/C4TwDgpgBAKgFgSwHYHMoF4BQUoB8oDe2OJokAXFAOQCGVA3MSVAPZIA2IAgpQM7AAnZCkbMAvsXxFmOMhEpUARlTzUAQgyYk2nNX0HDRJMY0wBjNvyjBEqSvGEZCYqDV6xbIzJl4B3BMBmcFAAFIIArhAAlITEZm7QNsIAdHIY6OjUylAAZDnWnsk6IGrpmbRU5HEJBSlpGeV0uflJqEUc3GVZldW8iYX1GdRNebVtxVxdVGY9zIoCEDQA1qY48X1jKKng0A3qsyTziyveOAAmEABmNOHswFVzC8uMYkA

and if they are all listed as individual case statements, they also work:

https://www.typescriptlang.org/play?ts=4.3.0-dev.20210331#code/C4TwDgpgBAKgFgSwHYHMoF4BQUoB8oDe2OJokAXFAOQCGVA3MSVAPZIA2IAgpQM7AAnZCkbMAvsXxFmOMhEpUARlTzUAQgyYk2nNX0HDRJMY0wBjNvyjBEqSvGEZCYqDV6xbIzJl4B3BMBmcFAAFIIArhAAlITEZm7QNsIAdHIY6OjqVORaigIQNADWpjjxvImeqeDQGZlKKgBkDdaVOiBq6XV0Ocx5BcXepQktKWm11HRQTSOoyW1cndTKPSR9RSVQZRWj1Yu0jc1Js-N7Ztm5+euDUAAmEABmNOHswCs4awNiQA

So this type of narrowing is supported by TS inside of a switch(true) statement, but for some reason the order and grouping of the case statements makes the difference here.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 31, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 31, 2021
@mhluongo
Copy link

Any progress here? Small but unfortunate bug for heavy union users

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants