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
Rule proposal: no-variable-declaration-type-superset #4738
Comments
Hm, now I can't reproduce the false-positive, but it's definitely happening in my local project. |
Indeed, this is an intentional -and good- result of TypeScript's type narrowing. The variable let nameMaybe: string | undefined = "dionysus";
// We know nameMaybe is `string` here, not `string | undefined`.
// TypeScript shouldn't yell at us that it might not be defined.
console.log(nameMaybe.toUpperCase());
if (Math.random() > 0.5) {
nameMaybe = undefined;
}
Because of the above, I don't think we'd want to take this rule into core. It's not uncommon for TypeScript code to intentionally use a wider type in the type annotation than the value. // Would be flagged by no-variable-declaration-type-superset
let status: "fail" | "pass" = "pass";
if (!riskyOperation() || !riskyOtherOperation()) {
status = "fail";
}
Have you tried enabling I'm going to close this for now as it's bit too niche of a use case for including in typescript-eslint core. If you'd like to create it yourself you can always use the steps in https://typescript-eslint.io/docs/development/custom-rules. Thanks for posting though! |
For the local project, strictNullChecks is already enabled. With copying in the TSConfig from the local project (which I initially forgot so strictNullChecks was not enabled), I still can't reproduce the issue in playground. Failed link again |
I'm quite confused why the playground repro does not work.
And the warning still gets reported in my local project. |
Maybe the same root cause as #4493? |
Hm, that seems quite plausible. Trying to "peek definition" on |
Do you mean Without a reproduction in front of me I can't speak to any projects in particular. If you can get your project up online that would be great! |
Whoops, yeah that's a typo on my part. As I said earlier, strictNullChecks is already enabled. The reproduction from the playground is almost an exact example, it just doesn't warn in the playground due to the issue you mentioned. In local projects, it creates a false-positive on |
That's a pretty niche use case. We wouldn't take it into typescript-eslint itself right now as very few people have mentioned wanting it so far. I'd recommend trying it out as a custom rule you write with the APIs documented at https://typescript-eslint.io/docs/development/custom-rules, and if you find it useful releasing it as an open source package. If it ends up being useful and popular we can always take it in. |
Interesting that no one has requested that before. I would've thought the undefined case specifically is a common enough occurence to want to make a configurable adjustment to the rule conditions. I'll have a look at the custom rule thing. Thanks for the help! |
Description
Currently, the following snippet produces a false-positive
no-unnecessary-condition
due to a TS limitation:where
foo
gets narrowed to justboolean
, despite the explicit type annotation. Whenfoo
is declared without type annotation, with a type assertion ofas boolean | undefined
instead, this false-positive does not occur. This rule will recommend the replacement style to avoid false positive rules due to TS type narrowing. It will only report on cases where the variable type assigned is a subset of the type annotation.I'm not really sure this rule is really the best solution, but it felt the most fitting. Alternatively,
no-unnecessary-condition
could get fine-grain control over which expressions and types it specifically reports on. For example, the case ofa !== undefined
is very common in generating this false-positive.Fail
Pass
The text was updated successfully, but these errors were encountered: