-
Notifications
You must be signed in to change notification settings - Fork 889
Rule suggestion: Ban comparisons of non-primitives #3574
Comments
Should be allowed, because it can be anything.
Get the constraint of the type parameter and apply the same logic as for all other types.
Is it an error if you compare |
Sounds good 👍
I'm not sure I understand? What about this function? Allowed or not? Or would each call site be checked individually? function foo<T>(x: T, y: T): boolean {
return x === y;
}
Perhaps, I personally don't mind either way. But I think there is an argument to be made not to have any special cases. Dates can be compared using getTime(). Date I think is the most common mistake where people compare them with ===, and it is very easy to make the mistake of changing a working ">" comparison to a broken "===" comparison (which of course this new rule would catch, but still there is an argument to be made in favor of encouraging consistency: always use getTime())
I would say that this should be an error. |
Let's take your example from above: function foo<T>(x: T, y: T): boolean {
return x === y;
}
|
I'm not sure I understand/agree with your analysis. According to that logic if we have a function: function foo<S, T>(x: S, y: T): boolean {
return x === y;
} Then |
Also: Think about how this rule should work with |
Has there been any progress regarding this issue? I currently ran into this problem when switching from A small example:
All of those were valid. I would expect tslint to give me a warning in case I would actually like the rule to only allow |
I started working on this last month. Hopefully this week I'll have time to go over the code again and submit a PR. I'm currently struggling with some basic questions:
But if you have two objects and would like to compare if it's the same reference, then that would not be possible:
I don't see any good way around that. It should probably be configurable. And in case of the "strict" mode, you can then do comparisons using Any thoughts on that? |
Here is my opinion: Ordering operators (
|
And here is another open question where I don't have an opinion or answer: GenericsHow should generics be handled? For instance, the following code: function checkFirstEqual<T>(a: T[], b: T[]): boolean {
const a1 = a[0];
const b1 = b[0];
return a1 === b1; // Legal or compile error?
}
console.log(checkFirstEqual([1,2,3], [4, 5, 6])); // Legal or compile error?
console.log(checkFirstEqual([{foo:1}], [{bar:2}])); // Legal or compile error? |
@benny-medflyt I submitted a PR. I would be happy to get some feedback. #4519 |
The comparison operators
===
,>
,<
,>=
,<=
can be used to compare primitive values, and this is usually good and fine.But when they are used to compare between non-primitives (objects, arrays, functions, etc..) they compare by reference, instead of by value. This is often done accidentally, when the programmer really meant to compare by value (using a ".equals" method, or a deep-equal library).
I propose a rule that bans all comparisons where the operands are a non-primitive type. This will catch accidents where the programmer made a mistake and meant to use ".equals" or a deep-equal library. Many code bases never have the need to compare for reference-equality, and for those that do, it is usually pretty rare, and can be done with a user-defined function such as "isSameObject(a, b)" that makes it much more explicit what check is being performed.
Questions:
How should
any
be treated?How should generic parameters be treated?
The text was updated successfully, but these errors were encountered: