A value is compared with itself to test for NaN
What it finds
x !== x and x === x: comparing a value with itself is true, or false, for every value except NaN, so the comparison is a test for NaN. Written that way it is easy to misread, and Number.isNaN(x) says the same directly.
The idiom is old and deliberate, which is why this rule is a matter of style. The fix, to Number.isNaN(x) or !Number.isNaN(x), keeps behaviour when the operand is a variable, which is read once instead of twice, and Number is the global one. Operands that call, construct, await or assign anything may give different values each time and are left alone, and so are comparisons in assertions.
From the rule's source, self_comparison.rs.
function check(value: number, items: number[]) { const nan = value !== value;tx0 reports: tests whether value is NaN const number = items[0] === items[0];tx0 reports: tests whether items[0] is NaN const other = value === items[0] || value < value; return nan && number && other;} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:self-comparison" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:self-comparison] reason="..."tx0 explain tx0/ts:self-comparison shows the same in the terminal. Found a false positive? Report it.