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

Control flow analysis: switch (true) case #8934

Closed
Strate opened this issue Jun 2, 2016 · 2 comments
Closed

Control flow analysis: switch (true) case #8934

Strate opened this issue Jun 2, 2016 · 2 comments
Labels
Suggestion An idea for TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@Strate
Copy link

Strate commented Jun 2, 2016

TypeScript Version:

1.9.0-dev

Code

// A self-contained demonstration of the problem follows...
interface A {}
interface B {}
interface C {}
declare function isA(arg: any): arg is A;
declare function isB(arg: any): arg is B;
declare function isC(arg: any): arg is C;

function handleAction<T>(action: T) {
  switch (true) {
    case isA(action):
    case isB(action):
      // action expected to be A|B type here
      break;
    case isC(action):
      // action expected to be C type here
      break;
    default:
      // action expected to be undefined type here
  }
}
@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 2, 2016
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.1 milestone Jun 7, 2016
@sandersn
Copy link
Member

Unfortunately, the switch code uses a form that is not recognised as a narrowing form. switch is really only used to narrow discriminated unions:

function f(x: First | Second | Third) {
  switch(x.kind) {
    case Kind.First:
      x; // x is now First
      break;
      // etc
  }
}

Note that even if you rewrite to if, not all expressions are usable for narrowing. For example,

if (true === isA(action)) {
  action; // action is not narrowed
}
if (isA(action)) {
  action; // action is narrowed to A
}

In order to keep things simple, narrowing only supports a few common syntactic forms. We do expand this list from time to time, but I don't think the switch (true) pattern is common enough to make it on the list.

@sandersn sandersn added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 13, 2016
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Bug A bug in TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug labels Oct 13, 2016
@DanielRosenwasser DanielRosenwasser removed this from the TypeScript 2.1 milestone Oct 13, 2016
@mhegazy mhegazy reopened this Oct 13, 2016
@RyanCavanaugh RyanCavanaugh added Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it and removed In Discussion Not yet reached consensus labels Nov 15, 2016
@RyanCavanaugh
Copy link
Member

Not a common enough pattern to support

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

5 participants