Rules Hub
Code Ruleset
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)
An assertion can hide a missing or incompatible field instead of making the compiler check the object.
Objašnjenje (HR)
Type assertion može sakriti nedostajuće ili nekompatibilno polje umjesto da compiler provjeri objekt.
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.
Notes (EN)
When constructing a value with a known TypeScript contract, prefer a checked annotation or satisfies expression over an assertion that bypasses assignability.
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. A generic supplied to an unvalidated data source does not validate external values; validate those at runtime.
Iznimke / Tradeoffi (HR)
as const je prihvatljiv. Uski cast dopušten je kada raspolažeš informacijom koju sustav tipova nema, primjerice nakon provjere tijekom izvođenja, uz kratko obrazloženje. Generički tip nad neprovjerenim izvorom ne validira vanjske podatke; njih provjeri tijekom izvođenja.