Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Guard optional numbers with typeof, not !== undefined
When conditionally including an optional numeric field, guard with typeof x === 'number' instead of x !== undefined. The undefined check lets null (and NaN-via-coercion surprises) through; the typeof check admits only real numbers.
Bad example
| 1 | ...(logo.width !== undefined && { width: String(logo.width) }) |
| 2 | // logo.width === null -> emits width: "null" |
Explanation (EN)
x !== undefined is true for null, so a null value slips through and gets stringified into bad output.
Objašnjenje (HR)
x !== undefined je istinito za null, pa null vrijednost prodje i pretvori se u neispravan izlaz.
Good example
| 1 | ...(typeof logo.width === 'number' && { width: String(logo.width) }) |
Explanation (EN)
typeof x === 'number' admits only actual numbers, excluding both undefined and null.
Objašnjenje (HR)
typeof x === 'number' propusta samo stvarne brojeve, iskljucujuci i undefined i null.