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).
Don't rely on Boolean() to narrow a possibly-undefined value
Boolean(x) is not a type guard in TypeScript: after `Boolean(x) && ...` the value is still possibly undefined, so calling a method on it fails strict null checks (TS18048). Narrow with a truthiness guard (`x && ...` / `!!x && ...`) or an explicit null check before dereferencing.
Bad example
| 1 | const hasResolvedImage = Boolean(avatarUrl) && !avatarUrl.startsWith('{{'); |
| 2 | // error TS18048: 'avatarUrl' is possibly 'undefined' |
Explanation (EN)
Boolean(avatarUrl) returns a boolean and does not tell the compiler avatarUrl is a string, so the method call is rejected under strict null checks.
Objašnjenje (HR)
Good example
| 1 | const hasResolvedImage = avatarUrl && !avatarUrl.startsWith('{{'); |
Explanation (EN)
A plain `avatarUrl &&` truthiness guard narrows avatarUrl to string in the right-hand operand, so the method call type-checks — without a redundant !!.
Objašnjenje (HR)
Notes (EN)
Same applies to Boolean(x) used as an if-condition when you then access x. Prefer the truthiness form or a proper guard/optional chaining.