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

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

Closed
4 tasks done
ST-DDT opened this issue Oct 31, 2023 · 1 comment
Closed
4 tasks done
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ST-DDT
Copy link
Contributor

ST-DDT commented Oct 31, 2023

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:

@ST-DDT ST-DDT added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Oct 31, 2023
@ST-DDT
Copy link
Contributor Author

ST-DDT commented Oct 31, 2023

Duplicate of #2959

@ST-DDT ST-DDT closed this as completed Oct 31, 2023
@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
@Josh-Cena Josh-Cena added duplicate This issue or pull request already exists and removed triage Waiting for maintainers to take a look enhancement: plugin rule option New rule option for an existing eslint-plugin rule labels Nov 1, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants