Rules Hub
Coding Rules Library
Encapsulate internal types and helpers
Do not export types, functions, or constants that are only used locally within a module to avoid polluting the public API.
Bad example
| 1 | // UserProfile.tsx |
| 2 |
|
| 3 | // BAD: This interface is only used for internal state but is exported |
| 4 | export interface IInternalUserState { |
| 5 | lastLogin: Date; |
| 6 | loginCount: number; |
| 7 | } |
| 8 |
|
| 9 | export const UserProfile = () => { |
| 10 | // Internal state logic using IInternalUserState |
| 11 | return <div>...</div>; |
| 12 | }; |
Explanation (EN)
The interface `IInternalUserState` is implementation detail relevant only to this file. Exporting it creates a messy API surface and encourages other modules to depend on what should be private logic.
Objašnjenje (HR)
Sučelje `IInternalUserState` je implementacijski detalj relevantan samo za ovu datoteku. Njegov izvoz stvara neuredno javno sučelje i potiče druge module da ovise o onome što bi trebala biti privatna logika.
Good example
| 1 | // UserProfile.tsx |
| 2 |
|
| 3 | // GOOD: The interface is private (not exported) |
| 4 | interface IInternalUserState { |
| 5 | lastLogin: Date; |
| 6 | loginCount: number; |
| 7 | } |
| 8 |
|
| 9 | export const UserProfile = () => { |
| 10 | // Internal state logic using IInternalUserState |
| 11 | return <div>...</div>; |
| 12 | }; |
Explanation (EN)
Removing the `export` keyword ensures the type remains private to the module. This enforces encapsulation and allows you to refactor internal structures later without worrying about breaking external consumers.
Objašnjenje (HR)
Uklanjanje ključne riječi `export` osigurava da tip ostane privatan unutar modula. To nameće enkapsulaciju i omogućuje kasnije refaktoriranje internih struktura bez brige o tome hoće li se slomiti vanjski potrošači.