Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Add explicit types to variables whose value can be one of several types
Annotate a union or nullable variable when inference is too narrow for later assignments or an explicit contract is needed.
Bad example
| 1 | let cost = null; |
| 2 | cost = calculateCost(); |
Explanation (EN)
An initially null variable may need a declared domain for later numeric assignments.
Objašnjenje (HR)
Good example
| 1 | let cost: number | null = null; |
| 2 | cost = calculateCost(); |
Explanation (EN)
The annotation permits the later numeric result while retaining the missing-value state.
Objašnjenje (HR)
Notes (EN)
Add a union annotation when initialization infers a narrower type than later assignments require, or when an API contract needs to constrain the domain.
Exceptions / Tradeoffs (EN)
Do not repeat an already accurate number | null inference solely because the result is nullable. Public-contract readability can justify an annotation.