Skip to content

Discriminated Unions don't cast with single type #18056

Closed
@mindblight

Description

@mindblight

TypeScript Version: 2.3.4

Code

type Choices = {
  type: "A"
};

function chooser(action: Choices): boolean {
  switch (action.type) {
    case "A":
      return true;
    default:
      const exhaustivenessCheck: never = action;
      return false;
  }
}

Expected behavior:
Code compiles.

Actual behavior:
Receive Error:

test.ts(10,13): error TS2322: Type 'Choices' is not assignable to type 'never'.

Reasoning
For libraries like Redux, there are places where it makes sense to have a switch statement with a single case for Discriminated Unions. (e.g. in reducers with a single action)

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions