An operation has the same outcome whatever the values involved
What it finds
options ?? {} written as {} ?? options, or x === []: an expression whose outcome does not depend on the value it seems to test.
Two forms are reported, both decided by syntax alone:
- ?? whose left side can never be nullish, such as an object, array, function or class literal, a string, number or bigint literal, a template, or the result of an arithmetic, comparison or unary operator other than void; or whose left side is always nullish, null or void. The right side is then never used, or always. - === or !== with an object, array, function, class or regular expression literal on one side, which creates a new value that nothing else can be identical to.
From the rule's source, constant_binary_expression.rs.
function check(options: object | undefined, value: unknown, count: number) { const a = {} ?? options;tx0 reports: {} is never null or undefined const b = count + 1 ?? 0;tx0 reports: count + 1 is never null or undefined const c = null ?? value;tx0 reports: null is always null or undefined const d = value === [];tx0 reports: [] creates a new value, so this comparison is always false const e = {} !== value;tx0 reports: {} creates a new value, so this comparison is always true const fine = options ?? {} ?? value; const loose = value == []; return [a, b, c, d, e, fine, loose, (value ?? 1) && count];} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/ts:constant-binary-expression" = "off"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:constant-binary-expression] reason="..."tx0 explain tx0/ts:constant-binary-expression shows the same in the terminal. Found a false positive? Report it.