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

Never type error in switch default #10600

Closed
k8w opened this issue Aug 30, 2016 · 3 comments
Closed

Never type error in switch default #10600

k8w opened this issue Aug 30, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@k8w
Copy link

k8w commented Aug 30, 2016

TypeScript Version: 2.0.0 beta

Code

interface Clazz{
    type: 'A' | 'B' | 'C'
}

let obj:Clazz = {
    type: 'A'
};

switch(obj.type){
  case 'A':
    console.log(obj.type);
    break;
  case 'B':
    console.log(obj.type);
    break;
  case 'C':
    console.log(obj.type);
    break;
  default:
    console.log(obj.type);
    break;
}

Expected behavior:
Compile successful.

Actual behavior:
error TS2339: Property 'type' does not exist on type 'never'.
Can compile successfully without default

Suggestion:
Well I know never type is a good way.
But in this case, value may be comes from a web API response.
So wanna add some protection code here, but get an error.

@RyanCavanaugh
Copy link
Member

I just type assert back to the original type. It's not clear how TS can best help you here -- it ought to be an error to access members on something that "shouldn't happen", but sometimes you do want to use the discriminant to throw an exception.

One option is to make a separate thing like

function throwBadKind(x: never): never {
  throw new Error('Unknown kind ' + (x as YourThing).kind);
}

so you can write

   default:
     return throwBadKind(type);

@k8w
Copy link
Author

k8w commented Aug 30, 2016

Well as example above, if I use obj['type'] in default, it will compile successfully.

@gcnew
Copy link
Contributor

gcnew commented Aug 30, 2016

@twoeo This is likely to change soon if #10565 gets merged in.

On a side note, I really think the type system should not go into much effort for defensive programming. After all, you want a robust system which you can rely on without many exceptions.

In my experience, when TS code is called from JS, it is much better to have an interface/relay layer that makes sure the TS contracts are being held than having TS logic littered with dead-looking code.

@mhegazy mhegazy added Question An issue which isn't directly actionable in code Working as Intended The behavior described is the intended behavior; this is not a bug labels Aug 30, 2016
@mhegazy mhegazy closed this as completed Sep 20, 2016
@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
Question An issue which isn't directly actionable in code Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants