-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Code reachability contains False Positive in simple "always true" return condition #58455
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
Duplicate of #29323. |
Thank you @whzx5byb, that issue is from 2019, is it still a design limitation? I want to use some advanced type programming which figures out true/false values and have unreachability detection of it. |
@kungfooman I'm only a passerby so I can't tell. You have to wait for a TS team member to answer it. |
Quote from the other issue, emphasis mine:
That's a pretty fundamental design constraint, and I highly doubt anything has changed in that regard. Then again, TS can do control flow for |
Everything is of course in-principle solvable, but there aren't a lot of cases where people intentionally write |
@RyanCavanaugh I investigated type checks on ranges a bit through type programming doing arithmetic, e.g. a function could return a random number from 20 to 100. If later there is an condition which checks that this number is bigger than... lets say 200, it can only be false (and ideally be pointed out through dead-code detection). You already planned range based type flow in another PR and I just wanted to see if it's possible already without any extra PR's. But this issue makes range based type checks not too helpful as-is. OTOH it seems to already exist, but with an extra linting step: https://typescript-eslint.io/rules/no-unnecessary-condition/ |
The lint rule has a lot of false positives, e.g. const n = [1, 2];
if (n[4] !== undefined) { // <- thinks this is always true
} |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Uh oh!
There was an error while loading. Please reload this page.
🔎 Search Terms
🕗 Version & Regression Information
I don't know if this ever worked and it clearly doesn't work on v5.5.0-beta
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.5.0-beta&filetype=ts#code/GYVwdgxgLglg9mABFApgZygCgJSIN4BQiiECGiAhgDYDuFAnmgCoBOIKiAvMmygNxFEMYIkzU6jVu1yFixFiiggWYAcQC+g0mDRwqKAHRU4Ac0wAiJgAsYaRACo0VuCCoATe4gBGHEywYoboguUAbm2GpCIphQvDKC8orKqoKaxNq6+kamFta2DraefgFBIWERBJqoGDh8QA
💻 Code
🙁 Actual behavior
🙂 Expected behavior
Correctly identify unreachable code from the variable.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: