-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: prevent comparing object types #5647
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
I don't think I agree with the premise of both of these checks. Comparing strings using For objects similarly - there are many cases where comparing by reference is what you really want to do. One case in point is when you've got readonly object types - comparing by reference is exactly what you want to do. The point I'm getting at is that the code that this rule enforces is not always going to be the correct thing to do - not just at a "some people mightn't like this", but even within a single codebase you might need to both use and not use the rule. Looking at the history - the rule was a very late addition to tslint, which is why it's not covered in our rule alternative guide. This is the first request for the rule in 3 years, so I don't think it was a highly used rule, which doesn't indicate that it is useful for the broader TS community. I'd be interested in hearing @JoshuaKGoldberg's thoughts given he was involved in the original TSLint implementation. |
Yeah I'm not too motivated to add a rule that bans string The one case for this rule that I can see being useful is object greater than / less than equality: const object1 = {};
const object2 = {};
if (object1 < object2) {} Maybe we could accept a trimmed down version of the rule that just flags object greater-than / less-than checks? |
One big issue I see is also the most common mistake we see when people look at TS types.
function compare(a: {}, b: {}) {
return a < b;
}
compare({}, {}); // obviously bad
compare(() => {}, () => {}); // obviously bad
compare(1, 2); // completely fine
compare('a', 'b'); // mostly fine So the whole premise of disallowing Going one step further to prove my point - even a type that looks like an object still doesn't mean an object: function compare(a: {toString(): string}, b: {toString(): string}) {
return a < b;
}
compare({}, {}); // obviously bad
compare(() => {}, () => {}); // obviously bad
compare(1, 2); // completely fine
compare('a', 'b'); // mostly fine So really the only way this rule is safe and correct is if we ensure that for So I'm not sure there's any part of the rule that we are strongly in favour of. |
🙃 another victim of microsoft/TypeScript#9879. Marking as blocked until that one's resolved. |
FWIW I'd love to have this rule available for my own code. I can think of situations where I might want reference equality, but those are rare and I'd very much rather see a warning for it that I'd have to explicitly turn off. I'm not saying that people should be pushed towards enabling this rule — far from it. But I would appreciate having it. |
Here is why I would like something like what this feature request is driving at. I use tiny types all the time:
|
I agree that this rule should not be applied by default, but I think there are valid, common mistakes that catch folks out and that having the ability to enable this rule would be helpful for preventing inconsistencies and errors. Specifically, we were just caught out by the gotcha around |
Have to convert some code which previously used This rule would in general be extremely helpful when working with Maybe in the options for the rule, you can specify which types the rule should be used on. I would include (PS: I also need to fix the assignment by reference instead of value difference. However I am sure that asking for such a rule would be just plain rude 😃) |
See also discussion in #9335. |
Coming from that issue, sounds like we have consensus to proceed with banning the Special-casing/options around It also sounds like we have consensus amongst team members to reject the Note - the If other team members disagree with this path forward, please yell! 🙂 |
My thinking is that the rule should by default check all cases, with |
Now that v8 is released, per #7936, we can mark this issue as (now fully) unblocked! 🚀 |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
Many developers mistakenly use
===
or!==
to compare objects whereas they intend to useisEqual
or.equals
. The same for strings where most will want to uselocaleCompare
instead of operators<
,<=
,>=
,>
.I would like to port strict-comparisons rule from TSLint to here: https://palantir.github.io/tslint/rules/strict-comparisons/
Fail Cases
Pass Cases
Additional Info
No response
The text was updated successfully, but these errors were encountered: