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 shared helper to the common DTO, not one concrete subtype
If a helper is called with multiple DTOs that share fields, declare the parameter as the shared base/fields type rather than one specific variant.
Bad example
| 1 | // Called from both create (CreateOrderDto) and update (UpdateOrderDto) flows, |
| 2 | // but typed to only one — the create call site now needs a cast. |
| 3 | private resolveAmounts(dto: UpdateOrderDto) { /* uses only shared fields */ } |
Explanation (EN)
Typing to UpdateOrderDto when the helper only touches shared fields misrepresents the contract and forces a cast at the create call site.
Objašnjenje (HR)
Tipiziranje na UpdateOrderDto kada helper koristi samo zajednicka polja krivo prikazuje ugovor i prisiljava na cast na mjestu poziva za create.
Good example
| 1 | // Define a shared fields DTO that both Create and Update extend. |
| 2 | private resolveAmounts(dto: OrderFieldsDto) { /* uses only shared fields */ } |
| 3 | // Now createOrder(CreateOrderDto) and updateOrder(UpdateOrderDto) both pass without casts. |
Explanation (EN)
Declaring the parameter as the shared OrderFieldsDto accurately expresses what the helper needs and accepts both DTOs without casting.
Objašnjenje (HR)
Deklariranje parametra kao zajednickog OrderFieldsDto tocno izrazava sto helper treba i prihvaca oba DTO-a bez castanja.