Skip to content

Switch on enum of one member not considered exhaustive #12771

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
hmaurer opened this issue Dec 8, 2016 · 3 comments
Closed

Switch on enum of one member not considered exhaustive #12771

hmaurer opened this issue Dec 8, 2016 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@hmaurer
Copy link

hmaurer commented Dec 8, 2016

TypeScript Version: 2.1.4

Code:

// --strictNullChecks

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:

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

Without strict null checks, it compiles without error.

@RyanCavanaugh RyanCavanaugh changed the title Possible bug with exhaustivity checking in switch statement Switch on enum of one member not considered exhaustive Dec 8, 2016
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 8, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 8, 2016
@MaklaCof
Copy link

MaklaCof commented Jan 6, 2017

Is it possible to get information where error occurred? Right now there is no file, line information about that.
I just install fresh windows installation with VS2017RC and get this error. I would like to add default statement to switches (they already should be there, but apparently I miss one), but there are simply to many files to check it.

@mhegazy mhegazy modified the milestones: TypeScript 2.2, TypeScript 2.3 Feb 2, 2017
@mhegazy mhegazy modified the milestones: Future, TypeScript 2.4 May 11, 2017
@amilner42
Copy link

I have a related issue also with enums with one value, you can directly copy this to the microsoft typescript playground:

No error as expected for this snippet (2 values in enum)

enum Rating {
  Like = 1,
  Dislike
};

let a: Rating;

switch (a) {
  case Rating.Like:
    break;

  case Rating.Dislike:
    break;

  default:
    let s: never = a;
}

On the other hand, drop it down to one value in the enum and you get the error:

Type 'Rating' is not assignable to type 'never'.

enum Rating {
  Like = 1
};

let a: Rating;

switch (a) {
  case Rating.Like:
    break;

  default:
    let s: never = a; // error here
}

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Examples in this issue seems to be working as expected in latest.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.6, Future Aug 17, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 17, 2017
@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

6 participants