Skip to content

Never type error in switch default #10600

Closed
@k8w

Description

@k8w

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in codeWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions