Skip to content

Bug: [switch-exhaustiveness-check] invalid fix for union with infinite types #6682

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

Closed
4 tasks done
Josh-Cena opened this issue Mar 19, 2023 · 6 comments · Fixed by #8250
Closed
4 tasks done

Bug: [switch-exhaustiveness-check] invalid fix for union with infinite types #6682

Josh-Cena opened this issue Mar 19, 2023 · 6 comments · Fixed by #8250
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

@Josh-Cena
Copy link
Member

Josh-Cena commented Mar 19, 2023

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.0.1-rc&sourceType=module&code=CYUwxgNghgTiAEYD2A7AzgF3lAXPARFPvAD4EBGxZKArgLbkgwDcAUK2gO4CWGYAFvAAUUAJTwA3q3iIoaBIXx5ycKAGs2MsHIWVlqjawC+rIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6ZAd0vzIAt6AHrwCGsZPkoA3RExTJafRGQDW6KImjQA9tEjgwAXxAGgA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

declare const a: "a" | "b" | number;

switch (a) {
  case "a": break;
  case "b": break;
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/switch-exhaustiveness-check": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

No error, no suggestion fixer, or suggestion to add a default case

Actual Result

Fixer produces case number:, which is invalid syntax.

Additional Info

I'm not sure what the best fix for this issue is, so I hope to get more discussion first.

@Josh-Cena Josh-Cena added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 19, 2023
@Josh-Cena
Copy link
Member Author

Found another related instance:

declare const a: number | string;

switch (a) {
  case 2: break;
}

This should not report anything

@Josh-Cena Josh-Cena changed the title Bug: [switch-exhaustiveness-check] invalid fix for literal + infinite type union Bug: [switch-exhaustiveness-check] invalid fix for union with infinite types Mar 19, 2023
@Josh-Cena
Copy link
Member Author

Another interesting case:

declare const a: "a" | (string & {});

switch (a) {
  case "a": break;
}

This has a suggestion but suggestion produces an empty line

@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Mar 20, 2023
@Josh-Cena
Copy link
Member Author

Josh-Cena commented Mar 20, 2023

@bradzacher For the "a" | "b" | number case, do you think we should not report at all, or should we suggest to add a default? If the switch only handles "a", should the rule report that "b" is missing?

@bradzacher
Copy link
Member

Report if the literal cases are missing with no default, no auto or suggestion fixer provided.

Considering we ignore the number etc case normally - I'd say the rule should continue to ignore it?

@Josh-Cena
Copy link
Member Author

That's reasonable. So we filter out all infinite types from the union, and require the remaining literals to be exhaustively matched.

@bradzacher
Copy link
Member

bradzacher commented Mar 20, 2023

Also funny to note that the rule will similarly report on the type string | number | object and it'll suggest an equally broken fixer heh

Because TS's boolean type is secretly just an alias for true | false, it does handle it correctly (but that's a fluke heh)

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
2 participants