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).
Use a precise/indexed-access type instead of Record<string, unknown>
Don't widen to Record<string, unknown> when the exact shape is derivable; derive it (e.g. (typeof x)[number]).
Bad example
| 1 | const eventData: Record<string, unknown> = { |
| 2 | event: 'follow', |
| 3 | entityId, |
| 4 | }; |
| 5 | window.dataLayer.push(eventData); |
Explanation (EN)
Widening to Record<string, unknown> throws away type checking — typos and missing fields in eventData go undetected even though the target's element type is known.
Objašnjenje (HR)
Sirenje na Record<string, unknown> baca provjeru tipova — tipfeleri i polja koja nedostaju u eventData prolaze neopazeno iako je tip elementa odredista poznat.
Good example
| 1 | const eventData: (typeof window.dataLayer)[number] = { |
| 2 | event: 'follow', |
| 3 | entityId, |
| 4 | }; |
| 5 | window.dataLayer.push(eventData); |
Explanation (EN)
Deriving the element type from the destination keeps eventData fully type-checked against the shape it must satisfy.
Objašnjenje (HR)
Izvodenje tipa elementa iz odredista zadrzava potpunu provjeru tipa eventData prema obliku koji mora zadovoljiti.