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).
Don't use a type assertion where you need a real runtime conversion
`as number` only fools the compiler; parse/coerce values from untyped sources with Number()/parseInt().
Bad example
| 1 | const data = JSON.parse(raw) as Record<string, unknown>; |
| 2 | const id = data.id as number; // still a string at runtime if JSON had "42" |
| 3 | const next = id + 1; // "421" instead of 43 |
Explanation (EN)
A `as number` assertion is erased at compile time. If the JSON actually held a string, `id` is still a string at runtime and arithmetic produces garbage.
Objašnjenje (HR)
Tvrdnja `as number` brise se pri kompajliranju. Ako je JSON zapravo sadrzavao string, `id` je u izvodenju i dalje string pa aritmetika daje besmislice.
Good example
| 1 | const data = JSON.parse(raw) as Record<string, unknown>; |
| 2 | const id = Number(data.id); // actually converts |
| 3 | if (!Number.isFinite(id)) throw new Error('invalid id'); |
| 4 | const next = id + 1; |
Explanation (EN)
`Number()` performs a real runtime conversion, and a finiteness check guards against NaN before the value is used.
Objašnjenje (HR)
`Number()` obavlja stvarnu pretvorbu u izvodenju, a provjera konacnosti stiti od NaN prije koristenja vrijednosti.
Exceptions / Tradeoffs (EN)
When you already know the value's runtime type with certainty (e.g. it came from a typed in-memory object), an assertion is fine and conversion is redundant.
Iznimke / Tradeoffi (HR)
Kada vec sa sigurnoscu znas tip vrijednosti u izvodenju (npr. dolazi iz tipiziranog objekta u memoriji), tvrdnja je u redu i pretvorba je suvisna.