Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP1universalStack: typescript
typescriptnull-safetytype-assertioncorrectness
Guard possibly-undefined values instead of asserting the type
When a value can legitimately be undefined, render/handle the guarded case rather than forcing it with `as Type`, which hides a real optionality mismatch and risks runtime errors.
PR: hegnar-journalist-boost · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetsx
| 1 | // article is undefined in 'new' mode, but forced anyway |
| 2 | <Panel article={article as Article} /> |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | {article ? <Panel article={article} /> : <EmptyState />} |
| 2 | // or make the prop optional and guard field access inside Panel |
Explanation (EN)
Objašnjenje (HR)