Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Unintuitive behaviour of exhaustive switch with widening literals #12529

Copy link
Copy link
@crobi

Description

@crobi
Issue body actions

TypeScript Version: both 2.1.1 and nightly (2.2.0-dev.20161127)

Code

// --strictNullChecks

class A {
  readonly kind = "A"; // (property) A.kind: "A"
}

class B {
  readonly kind = "B"; // (property) B.kind: "B"
}

function f(value: A | B): number {
  switch(value.kind) {
    case "A": return 0;
    case "B": return 1;
  }
  value; // (parameter) value: never
}

Expected behavior:

Compiles without errors.

Actual behavior:

Does not compile with TS2366: Function lacks ending return statement and return type does not include 'undefined'.

Note: The VS Code mouse over tooltips (shown as comments in the code above) show that the compiler has determined the type of the kind properties as string literals ("A" and "B", respectively), and it has determined that the end of function f is unreachable (value has type never).

As far as I can see, this has to do with widening/non-widening literals, as explicitly annotating the kind properties (e.g, readonly kind: "A" = "A") makes the code compile. It is not intuitive why this would affect TS2366, especially since the control flow analysis seems to have determined that the switch statement is exhaustive.

Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.