Skip to content

Unintuitive behaviour of exhaustive switch with widening literals #12529

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

Closed
crobi opened this issue Nov 28, 2016 · 3 comments
Closed

Unintuitive behaviour of exhaustive switch with widening literals #12529

crobi opened this issue Nov 28, 2016 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@crobi
Copy link

crobi commented Nov 28, 2016

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.

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Nov 28, 2016
@ahejlsberg ahejlsberg self-assigned this Nov 28, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Nov 28, 2016
@mhegazy mhegazy added this to the TypeScript 2.1.3 milestone Nov 28, 2016
@hmaurer
Copy link

hmaurer commented Dec 8, 2016

I have encountered an similar error:

enum Enum1 {
    A,
    B
}

enum Enum2 {
    A
}


interface Foo {
    readonly kind: Enum1
}

interface Bar {
    readonly kind: Enum2
}

let f = (x: Foo): number => {
    switch (x.kind) {
        case Enum1.A:
            return 1;
        case Enum1.B:
            return 2;
    }
}

let g = (x: Bar): number => {
    switch (x.kind) {
        case Enum2.A:
            return 1;
    }
}

Expected behaviour (as far as I know):

Compiles without error

Actual behaviour:

Does not compile with TS2366: Function lacks ending return statement and return type does not include 'undefined'. if "strict null checks" are enabled.

Without strict null checks, it compiles without error.

@RyanCavanaugh
Copy link
Member

@A3gis can you please log a separate bug? Thanks!

@hmaurer
Copy link

hmaurer commented Dec 8, 2016

@RyanCavanaugh yes of course, #12771

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants