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

Bug: [switch-exhaustiveness-check] fails with option "allowDefaultCaseForExhaustiveSwitch": false #8165

Closed
4 tasks done
danielrentz opened this issue Jan 2, 2024 · 5 comments · Fixed by #8176
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@danielrentz
Copy link

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.3.3&fileType=.tsx&code=GYVwdgxgLglg9mABFApgZygCjALkWEAWwCMUAnASkQG8BYAKEUTQHcYoIALRbKuxpoggBDNCkQBGPGRRQQZJACYA3A0FDR4xdNnykAFlUCmAExTBhIADZQdchYgBsRpgF8G7%2BkA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6ZAd0vzIAt6AHrwCGsZPkoA3RExTJafRGQDW6MAG1IiaNAD20SABpMUYfHi6OAEUQAzUfHwBhYckQAxfQFEho8VMQAZS4eXjV7eDcwAF8AXXAYkGigA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function test(n: number) {
  switch (n) {
    case 1: return 2;
    case 2: return 4;
    default: return 6;
  }
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  "rules": {
    "@typescript-eslint/switch-exhaustiveness-check": ["error", { "allowDefaultCaseForExhaustiveSwitch": false }]
  }
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

No error.

Actual Result

The switch statement is exhaustive, so the default case is unnecessary. 5:5 - 5:23

Additional Info

No response

@danielrentz danielrentz added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jan 2, 2024
@ehoogeveen-medweb
Copy link
Contributor

In fact, with "requireDefaultForNonUnion": true also set it is not possible to write a compliant switch:

  • with default gives "The switch statement is exhaustive, so the default case is unnecessary. 5:5 - 5:23"
  • no default gives "Switch is not exhaustive. Cases not matched: default 2:11 - 2:12"

I wonder if the problem is that the switch statement is exhaustive only because of the default - so its presence makes it redundant, and its absence makes it necessary.

@Josh-Cena Josh-Cena added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Jan 2, 2024
@Josh-Cena
Copy link
Member

An infinite type definitely needs a default. I'm surprised this isn't covered by a test—cc @Zamiell

@Zamiell
Copy link
Contributor

Zamiell commented Jan 2, 2024

That looks like a bug, I'll investigate.

@Zamiell
Copy link
Contributor

Zamiell commented Jan 2, 2024

should be fixed in this PR, sorry about that

@danielrentz
Copy link
Author

Thank you for the quick fix!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
4 participants