Skip to content
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

Exhausting enum values in switch statements without default case. #8618

Closed
tinganho opened this issue May 16, 2016 · 5 comments
Closed

Exhausting enum values in switch statements without default case. #8618

tinganho opened this issue May 16, 2016 · 5 comments

Comments

@tinganho
Copy link
Contributor

tinganho commented May 16, 2016

For expressivity I don't always include a default case in my switch statements. But I know that variable abc below cannot be any value other than a, b and c. Thus, I think the below code should not error, because I return the correct type for all branches it can take.

let abc: 'a' | 'b' | 'c';
function test(): string {// function lacks ending return statement and return type does not include 'undefined'.
    switch(abc) {
        case 'a':
            return '';
        case 'b':
            return '';
        case 'c':
            return '';
    }
}

And for enums:

enum ABC {
  a,
  b,
  c,
}
let abc: ABC
function test(): string {// function lacks ending return statement and return type does not include 'undefined'.
    switch(abc) {
        case ABC.a:
            return '';
        case ABC.b:
            return '';
        case ABC.c:
            return '';
    }
}
@kitsonk
Copy link
Contributor

kitsonk commented May 16, 2016

Dupe of #2214? Currently planned for TS 2.1...

@tinganho tinganho changed the title Control flow branching of string literals in switch statements without default case. Control flow branching of enums in switch statements without default case. May 16, 2016
@tinganho tinganho changed the title Control flow branching of enums in switch statements without default case. Exhausting enum values in switch statements without default case. May 16, 2016
@tinganho
Copy link
Contributor Author

@kitsonk IMO little bit different. That one is about types. My OP is about exhausting enum values. But anyway changed title and the content of my OP to reflect my intent.

@DanielRosenwasser
Copy link
Member

Hm, looks closer to #8602 actually.

@zpdDG4gta8XKpMCd
Copy link

might be related #6155

@tinganho
Copy link
Contributor Author

Yes, dupe of #6155.

@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
None yet
Projects
None yet
Development

No branches or pull requests

4 participants