Skip to content

[switch-exhaustiveness-check] Require default case if discriminant type is not a union #2959

Closed
@raymondwang

Description

@raymondwang
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/switch-exhaustiveness-check": "warn"
  }
}
type Day = string; // or number

const day = 'Monday' as Day;
let result = 0;

switch (day) {
  case 'Monday': {
    result = 1;
    break;
  }
}

or

type Day = any; // or unknown, or a JS variable outside of a TS context

const day = 'Monday' as Day;
let result = 0;

switch (day) {
  case 'Monday': {
    result = 1;
    break;
  }
}

Expected Result

I would expect to see an error message compatible with the default-case ESLint rule, indicating that the switch-exhaustiveness-check rule is invalidated because the expression is not a union type.

Actual Result

No error message.

Additional Info

Essentially, I think the best of both worlds is to just have the predictable functionality of the default-case rule operate normally except in the event that a switch statement's expression is a union type. Today, there doesn't seem to be any way for the two rules to operate in tandem. Adding this functionality might be a breaking change, but I'm honestly a bit curious how controversial it would be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions