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).
Annotate the variable's type instead of using `as`
Replace `as` casts with a type annotation on the declaration so the compiler verifies the value.
Bad example
| 1 | const emptyFollowing = { |
| 2 | category: [], |
| 3 | person: [] as number[], |
| 4 | author: [], |
| 5 | }; |
Explanation (EN)
`as number[]` overrides inference for one field and gives no guarantee the rest of the object matches the intended shape; casts suppress errors instead of surfacing them.
Objašnjenje (HR)
`as number[]` nadjacava zakljucivanje za jedno polje i ne jamci da ostatak objekta odgovara zeljenom obliku; castovi guse greske umjesto da ih otkrivaju.
Good example
| 1 | const emptyFollowing: Following = { |
| 2 | category: [], |
| 3 | person: [], |
| 4 | author: [], |
| 5 | }; |
Explanation (EN)
Annotating with the target type makes the compiler check every field against the contract — no unsafe escape hatch.
Objašnjenje (HR)
Anotacija ciljnim tipom tjera kompajler da provjeri svako polje prema ugovoru — bez nesigurnog izlaza.
Exceptions / Tradeoffs (EN)
`as const` is fine. A narrow cast is acceptable only when you genuinely have more information than the type system (e.g. after a validated runtime check) and a comment explains why.
Iznimke / Tradeoffi (HR)
`as const` je u redu. Uski cast prihvatljiv je samo kad stvarno imas vise informacija od sustava tipova (npr. nakon validirane runtime provjere) i komentar objasnjava zasto.