-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-fallthrough] eslint switch case doesn't respect never
#7641
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
Comments
This is expected because the rule is not type-aware. We'd need to build an extension rule. This is a pretty rare usecase so I'm not sure if it makes sense for us to own and maintain an extension rule for it. This is the first time in the history of the project that someone has requested this behaviour - so it's either not a common pattern, or just not a common pattern in codebases that use the rule. The closest issue we've had filed is #3455 (which is similar but A LOT more complicated). I'll leave this as "evaluating community engagement", but my gut is that we probably won't action this due to how rare it is. |
Totally fair. I hadn't see #3455 when I searched, but I'd be okay if you want to close this as duplicate of that. To add some missed details:
|
Yeah for sure - your want makes sense because there's no way to satisfy the lint rule and TS at the same time with code when using this pattern. But you can satisfy the rule by adding a comment. By default the rule will ignore cases when there is a comment that matches the regex I.e. you can add the comment But this is obviously sub-par in comparison to the rule just working. |
@bradzacher fwiw I was just about to file this bug as well, thereby making it half as rare now 🙂 |
In Next.js there are e.g. function getUserType(): "unauthorized" | "normal" | "admin" {
return "normal";
}
export default function MyPage() {
const userType = getUserType();
switch (userType) {
case "unauthorized":
redirect("/login");
break; // <-- problem here
case "normal":
return <div>Welcome, user</div>;
case "admin":
return <div>Welcome, admin</div>;
}
} |
Uh oh! @Eldemarkki, the image you shared is missing helpful alt text. Check #7641 (comment). Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image. Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs. |
@Eldemarkki your case is easily satisfiable by prefixing it with This has the added benefit of also making it very clear that |
Duplicate of #3455 |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.2.2&fileType=.tsx&code=PTAEFMGcBsEsDsAuBaAJrSBDARtcz5wAPFOQ0AAUQE8AHKAYwCdZaUozFh4B7ZAV3j9I4VMgBumJpABQxWjyaJQAM0ENEsHvAjxI-JuABy4ceCYAKAPqTo-cAC5QhM0wCUTl%2BdABvGaFBEAAsmHgB3Z3AIgFEmUMsAIgBVeCDMeFQ8VFB0SGZYAFsETERRUEEtHQLwAuxzBLcAbhkAXxkZNXgNSsCoRAtMT35a8w9nYbqmX39QSDDYRAYg0AG3aYCAhkwRUABGJz8NjYZtSGVsUABeUATSs4Tmo4DYFRWLy%2Bvbvob1p4DDRAGHS7R5PNp-cB6AzGUzmCzYJqgECgAA8yGQgSC4FAAAMvEwcaAAUDAnRsZD9IZMRhQDTSkwivBMNBVIpMdjOt1tAAaWYIBjY4I05lhTDUSCY0JhWRHcHHbbYgBMTmRaIxQolZ1g0BZoqQEswoGwhkwAGtQDgeGZMTx%2BABzIIzI7Epg6RWguUunQATl9zTaMju-QAzE0gA&eslintrc=N4KABBYEQKYB4BcYDsAmBnKAuMBtckEs6ANgJbIJYBOMAxgPYC2TKqMqUANAYVAA4kArgHMKWAAIIAnvxjo61MvwQBaeeUoB6DRTW1GLNhyi8wAXRABfIA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
I expect the break statement isn't necessary since the prior case block terminated in an expression returning
never
(that always throws).Actual Result
On line 15
Additional Info
No response
The text was updated successfully, but these errors were encountered: