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).
Avoid redundant type assertions when inference already gives the right type
Remove `as Type` casts when the expression already has that type; the cast is noise and can mask real type errors.
Bad example
| 1 | // dto.currency is already Currency | undefined, and the default is a Currency, |
| 2 | // so the result is already Currency — the cast is redundant. |
| 3 | const currency = (dto.currency ?? Currency.BASE) as Currency; |
Explanation (EN)
Both operands of ?? are already Currency, so the assertion adds nothing and would silently suppress a real mismatch if the types later diverge.
Objašnjenje (HR)
Oba operanda ?? su vec Currency, pa tvrdnja ne dodaje nista i tiho bi prikrila stvarni nesklad ako se tipovi kasnije razidu.
Good example
| 1 | const currency = dto.currency ?? Currency.BASE; |
Explanation (EN)
Letting inference produce the type keeps the code clean and preserves type-checking if the operand types ever change.
Objašnjenje (HR)
Prepustanje inferenciji da odredi tip cini kod cistim i cuva provjeru tipova ako se tipovi operanada ikad promijene.
Exceptions / Tradeoffs (EN)
Keep an assertion when you genuinely know more than the compiler (e.g., narrowing an `unknown` from an external boundary) — but then prefer a validated parse over a bare cast.
Iznimke / Tradeoffi (HR)
Zadrzi tvrdnju kad stvarno znas vise od kompajlera (npr. suzavanje `unknown` s vanjske granice) — ali tada radije koristi validirani parse umjesto golog casta.