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).
Type a config constant as the literal union its consumer accepts, not string or number
A constants object typed with string and number lets a typo or a wrong-scale value compile and pass lint, and these constants are often exactly the ones carrying a contract guarantee. Type each field as the union the consuming API actually accepts.
Bad example
| 1 | export const AVATAR_PARAMS: { type: string; quality: number } = { |
| 2 | type: "wepb", // typo compiles |
| 3 | quality: 850, // out of range, compiles |
| 4 | }; |
Explanation (EN)
Nothing catches either mistake until the generated URL is inspected by hand, and these two values carry the whole guarantee of the change.
Objašnjenje (HR)
Nista ne hvata nijednu od tih pogresaka dok se generirani URL ne pregleda rucno, a te dvije vrijednosti nose cijelu garanciju promjene.
Good example
| 1 | export const AVATAR_PARAMS: { type: "png" | "webp" | "jpeg"; quality: number } = { |
| 2 | type: "png", |
| 3 | quality: 100, |
| 4 | }; |
Explanation (EN)
The typo becomes a compile error, and the accepted values are documented by the type instead of by a comment.
Objašnjenje (HR)
Tipfeler postaje greska pri prevodenju, a prihvacene vrijednosti dokumentira tip umjesto komentara.
Notes (EN)
The consuming function usually already constrains its parameters this way. Reuse that union rather than restating a looser type at the constant.
Bilješke (HR)
Funkcija koja to konzumira obicno vec ogranicava svoje parametre na taj nacin. Iskoristi tu uniju umjesto da uz konstantu ponovis labaviji tip.