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

unreachable code detection for switch cases out of range of string literal types #6149

Closed
zpdDG4gta8XKpMCd opened this issue Dec 18, 2015 · 10 comments
Labels
Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

type foo = 'hey' | 'nay';

switch (<foo>undefined) {
    case 'boom': console.log('never the case'); break; // expected unreachable code, actual no problem
}
@RyanCavanaugh
Copy link
Member

This is somewhat surprising -- we should be issuing an error that boom isn't assignable to 'hey'|'nay'. @DanielRosenwasser what's your take?

@DanielRosenwasser
Copy link
Member

"boom" is a string, and "hey" | "nay" are assignable to strings, so you can check against the two.

We could contextually type case expressions, but how exactly is this different from (<foo>undefined) === "boom"? I'd love to fix this, but it's not clear how. This needs a proposal first.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Dec 18, 2015
@zpdDG4gta8XKpMCd
Copy link
Author

In this case "boom" isn't just a string (expression), but a string literal which we can immediately match against known cases, don't we?

@DanielRosenwasser
Copy link
Member

Check out #6196 which will make this do exactly what you want.

@zpdDG4gta8XKpMCd
Copy link
Author

I will, thanks a lot!

@zpdDG4gta8XKpMCd
Copy link
Author

it works!

let baz : 'foo' | 'bar';
switch (baz) {
   case 'baz': console.log('hey!');break;
}

gives

test.ts(3,9): error TS2322: Type '"baz"' is not assignable to type '"foo" | "bar"'.
  Type '"baz"' is not assignable to type '"bar"'.

looking to see it merged in

@DanielRosenwasser DanielRosenwasser added the Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined label Feb 18, 2016
@pocesar
Copy link

pocesar commented Feb 22, 2016

1.8 isn't complaining about a declared type of the following:

// in a node_module with a typings field
export declare type OrderStatus = 'CREATED' | 'WAITING' | 'PAID' | 'NOT_PAID' | 'REVERTED';
                scope['status'] = (item: IModelTransaction) => {
                    switch (item.status) {  // where > item.status: OrderStatus;
                        case 'CRATED': // not complaining and compiling
                            return 'Criado';
                        case 'PAID':
                            return 'Pago';
                        case 'WAITING':
                            return 'Aguardando';
                        case 'REVERTED':
                            return 'Estornado';
                        case 'NOT_PAID':
                            return 'Não pago';
                    }
                };

@DanielRosenwasser
Copy link
Member

Yes, the issue won't be fixed in 1.8

@RyanCavanaugh
Copy link
Member

@DanielRosenwasser are the labels on this issue correct?

@ahejlsberg
Copy link
Member

This issue is fixed with #9407.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Aug 3, 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
Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants