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 widen a shared type with a required field for a single use case
Adding a required field to a widely-used shared type can silently misrepresent other consumers that don't have that field.
Bad example
| 1 | // Used across many features: forms, tables, autocomplete, etc. |
| 2 | export type TagItem = { |
| 3 | id: number; |
| 4 | name: string; |
| 5 | urlname: string; |
| 6 | iconUrl: string; // newly added, required — but only one feature actually has icons |
| 7 | }; |
Explanation (EN)
Making `iconUrl` required on a broadly shared type forces every existing consumer to somehow provide it, even though most call sites never had icon data. This either breaks those call sites or invites fake placeholder values just to satisfy the type.
Objašnjenje (HR)
Ako `iconUrl` postane obavezno polje na tipu koji se koristi na puno mjesta, svi postojeći korisnici moraju nekako osigurati tu vrijednost, iako većina njih uopće nema podatke o ikoni. To ili ruši postojeći kod ili tjera na lažne placeholder vrijednosti samo da tip prođe.
Good example
| 1 | export type TagItem = { |
| 2 | id: number; |
| 3 | name: string; |
| 4 | urlname: string; |
| 5 | }; |
| 6 |
|
| 7 | // Derived type for the one feature that actually needs icons |
| 8 | export type TagItemWithIcon = TagItem & { iconUrl: string }; |
Explanation (EN)
The shared `TagItem` type stays untouched for existing consumers, and a new derived type adds `iconUrl` only where it's genuinely needed. This keeps the base type accurate for everyone and makes the icon requirement explicit and localized.
Objašnjenje (HR)
Zajednički tip `TagItem` ostaje nepromijenjen za postojeće korisnike, a novi izvedeni tip dodaje `iconUrl` samo tamo gdje je to stvarno potrebno. Tako osnovni tip ostaje točan za sve, a zahtjev za ikonom je eksplicitan i lokaliziran.
Exceptions / Tradeoffs (EN)
If every consumer of the shared type genuinely needs the new field, adding it directly (required) is fine — the concern is specifically about widening a type for one narrow use case.
Iznimke / Tradeoffi (HR)
Ako baš svaki korisnik zajedničkog tipa stvarno treba novo polje, u redu je dodati ga direktno kao obavezno — problem je specifično širenje tipa zbog jednog uskog slučaja korištenja.