Skip to content

Enhancement: [switch-exhaustiveness-check] require default cases for non-union types #7862

Closed as not planned
@ST-DDT

Description

@ST-DDT

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/switch-exhaustiveness-check/

Description

I propose that the switch-exhaustiveness-check checks switch statements that operate on non-union-types and require them to have a default case to ensure they are exhaustive.

Fail

const value: number = Math.floor(Math.random() * 3);
switch (value) {
  case 0:
    console.log("zero");
    return;
  case 1:
    console.log("one");
    return;
}

Pass

const value: number = Math.floor(Math.random() * 3);
switch (value) {
  case 0:
    console.log("zero");
    return;
  case 1:
    console.log("one");
    return;
  default:
    console.log("higher");
    return;
  // Or
  default:
    // Do nothing
}

Additional Info

Since it would break projects that have the switch-exhaustiveness-check enabled, but don't have all their switch statements fully exhaustive, this should probably locked behind a default false option. Maybe this could be turned on by default with the next major version.

This option can not be replaced with default-case due to #7539.

Originally searched for during:

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existspackage: 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