Pattern: Expression statement without effect
Issue: -
Expressions that don't affect program state are likely mistakes. These include standalone type expressions, type assertions, non-null assertions, and other expressions that aren't used in assignments, function calls, or other meaningful operations.
Example of incorrect code:
'use strict'; // not in directive position
a + b;
new Set<number>;
obj?.prop;
condition ? 'a' : 'b';
void 0;
x == y;
// TypeScript
type;
foo as string;
value!;
Example of correct code:
const result = a + b;
const set = new Set<number>();
const value = obj?.prop;
const str = condition ? 'a' : 'b';
const comparison = x == y;
// Side effects are ok
alert('hi');
[1, 2, 3].forEach(n => console.log(n));
// Assignment expressions
x = 0;
x += 1;
x++;