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).
Keep string literal union types in sync with actual usage
Add or remove union members to match what the codebase actually produces/consumes, not what it used to.
Bad example
| 1 | export type OrderSource = 'web' | 'app' | 'partner'; |
| 2 |
|
| 3 | // Elsewhere in the codebase, a new source is emitted but was never added to the type: |
| 4 | function trackOrder(source: OrderSource) { |
| 5 | // 'kiosk' gets cast or force-typed to satisfy the compiler |
| 6 | } |
| 7 | trackOrder('kiosk' as OrderSource); |
Explanation (EN)
The union type is missing a value that is genuinely produced elsewhere, forcing an unsafe cast (`as OrderSource`) at the call site. This defeats the purpose of the union type and hides a real gap from the type checker.
Objašnjenje (HR)
Union tip ne sadrži vrijednost koja se stvarno negdje generira, pa se na mjestu poziva mora koristiti nesiguran cast (`as OrderSource`). Time se poništava svrha union tipa i sakriva stvarna rupa od type checkera.
Good example
| 1 | export type OrderSource = 'web' | 'app' | 'partner' | 'kiosk'; |
| 2 |
|
| 3 | function trackOrder(source: OrderSource) { |
| 4 | // no cast needed, 'kiosk' is a valid, checked value |
| 5 | } |
| 6 | trackOrder('kiosk'); |
Explanation (EN)
The union type is updated to include every value actually in use, and any value no longer used is removed. This keeps the type an accurate, enforceable contract instead of stale documentation.
Objašnjenje (HR)
Union tip je ažuriran tako da uključuje sve vrijednosti koje se stvarno koriste, a svaka vrijednost koja se više ne koristi je uklonjena. Tako tip ostaje točan i provjerljiv ugovor, a ne zastarjela dokumentacija.
Notes (EN)
Applies to both adding a missing value and removing a value that's no longer emitted anywhere.
Bilješke (HR)
Vrijedi i za dodavanje vrijednosti koja nedostaje i za uklanjanje vrijednosti koja se više nigdje ne koristi.