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).
Fix the shared/canonical type, not a private local duplicate of it
Changing a type only on a file-local copy leaves every other consumer of the real, exported type unprotected.
Bad example
| 1 | // local, private copy of a type that already exists elsewhere in the codebase |
| 2 | interface IAsset { |
| 3 | renditions?: { |
| 4 | highRes?: { uri: string }; |
| 5 | }; |
| 6 | } |
| 7 |
|
| 8 | // meanwhile the shared, exported type every other consumer imports is untouched: |
| 9 | // export interface IAsset { renditions: { highRes: { uri: string } } } |
Explanation (EN)
This fixes the type check only for the one file that happens to declare its own local copy of `IAsset`. Every other place in the codebase that imports the real, shared `IAsset` still assumes `renditions.highRes` is always present, so the same class of bug can reappear there with no compile-time warning.
Objašnjenje (HR)
Ovo popravlja provjeru tipova samo za jedan file koji slučajno deklarira vlastitu lokalnu kopiju `IAsset`. Svako drugo mjesto u kodu koje uvozi pravi, dijeljeni `IAsset` i dalje pretpostavlja da `renditions.highRes` uvijek postoji, pa se ista vrsta bug-a može ponovno pojaviti tamo bez ikakvog upozorenja u vrijeme kompilacije.
Good example
| 1 | // fix the shared, exported type so every consumer benefits |
| 2 | import { IAsset } from './shared-types'; |
| 3 | // shared-types.ts: renditions.highRes is now optional in the single source of truth |
| 4 |
|
| 5 | function getHighResUri(asset: IAsset) { |
| 6 | return asset.renditions?.highRes?.uri; |
| 7 | } |
Explanation (EN)
Fixing and reusing the single, shared type means every consumer — not just the one file being edited — gets the corrected shape and the compiler's protection against the same mistake.
Objašnjenje (HR)
Popravkom i ponovnom upotrebom jednog dijeljenog tipa svaki potrošač tog tipa — ne samo file koji se trenutno uređuje — dobiva ispravljen oblik podataka i zaštitu kompajlera od iste greške.
Notes (EN)
This protection is only real if consumers actually use the typed value — code that receives the data as `any` or `any[]` bypasses the type checker entirely regardless of how correct the interface is, so fixing the type is necessary but not sufficient.
Bilješke (HR)
Ova zaštita je stvarna samo ako potrošači zaista koriste tipiziranu vrijednost — kôd koji prima podatke kao `any` ili `any[]` u potpunosti zaobilazi provjeru tipova bez obzira koliko je sučelje ispravno, pa je popravak tipa nužan, ali ne i dovoljan.