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 use @Inject when the DI container can infer the token from the type
Drop @Inject() for constructor params whose class type is itself a resolvable provider token; reserve it for interfaces, primitives, and named/custom providers.
Bad example
| 1 | @Injectable() |
| 2 | export class OrderService { |
| 3 | constructor( |
| 4 | @Inject(PaymentProvider) private readonly paymentProvider: PaymentProvider, |
| 5 | @Inject(ConfigService) private readonly configService: ConfigService, |
| 6 | ) {} |
| 7 | } |
Explanation (EN)
The class type already serves as the injection token, so @Inject is pure noise and implies a non-standard resolution is happening.
Objašnjenje (HR)
Tip klase vec sluzi kao token za injektiranje, pa je @Inject samo suvisan sum i sugerira da se dogada nestandardno razrjesavanje ovisnosti.
Good example
| 1 | @Injectable() |
| 2 | export class OrderService { |
| 3 | constructor( |
| 4 | private readonly paymentProvider: PaymentProvider, |
| 5 | private readonly configService: ConfigService, |
| 6 | ) {} |
| 7 | } |
Explanation (EN)
The container resolves the provider from the parameter type; the code is cleaner and conveys intent accurately.
Objašnjenje (HR)
Container razrjesava provider iz tipa parametra; kod je cisci i tocno prenosi namjeru.
Exceptions / Tradeoffs (EN)
Keep @Inject (or @Inject(TOKEN)) when injecting by interface, primitive value, or a named/custom provider token that cannot be inferred from the TypeScript type.
Iznimke / Tradeoffi (HR)
Zadrzi @Inject (ili @Inject(TOKEN)) kada injektiras prema sucelju, primitivnoj vrijednosti ili imenovanom/prilagodenom provideru ciji token se ne moze izvesti iz TypeScript tipa.