Skip to content

Switch on enum of one member not considered exhaustive #12771

Description

@hmaurer

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions