Typescript: Control flow analysis: `switch (true)` case

Created on 2 Jun 2016  路  2Comments  路  Source: microsoft/TypeScript

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
  }
}
Suggestion Won't Fix

All 2 comments

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.

Not a common enough pattern to support

Was this page helpful?
0 / 5 - 0 ratings