TypeScript 7.0 ships the native compiler as tsc and drops options deprecated in 6.0. Angular 22 still requires TypeScript >=6.0.0 <6.1.0, so both rule sets now warn against moving Angular projects to TS 7, and the Angular index asks to check the Angular version first. TypeScript runtime safety adds error handling (Error objects only, cause chaining, unknown in catch, one error model per layer, no swallowed errors) plus exact decimals for money, ISO 8601 UTC dates, and Intl formatting. Angular adds per-route SSR render modes, a global ErrorHandler with provideBrowserGlobalErrorListeners, and translatable text with locale-aware formatting. Generic guidance that the TypeScript, refactoring, and UI/UX rules already cover is removed from the Angular rules, which shrink by about 7.5 percent despite the additions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2.1 KiB
2.1 KiB
Angular Dependency Injection, Services, and HTTP
Apply these rules when creating Angular services, configuring dependency injection, or calling HTTP APIs. The TypeScript modeling and runtime-safety rules cover contracts and validation; this file adds Angular specifics.
Dependency injection and services
- Prefer the
inject()function over constructor parameter injection, as the Angular style guide recommends since v20; keep constructor injection where the surrounding code consistently uses it. - Scope services intentionally. Use root singletons only for truly shared, stateless, or coordinated state, and never store request- or component-specific mutable state in them.
- Use injection tokens for configuration or real abstractions; do not add interfaces only to satisfy dependency injection.
- Keep services focused, expose readonly state to consumers, and separate API access, domain logic, and UI state where practical. Do not use services merely to move poorly structured component code elsewhere.
HTTP and APIs
- Keep API access centralized in typed data-access services, and keep DTOs separate from domain models when their responsibilities differ.
- The type parameter of
HttpClient.get<T>()orhttpResource<T>()is a compile-time assertion, not validation. Treat responses asunknownand validate them at the data-access boundary before they drive authorization, persistence, navigation, or rendering. - Configure HTTP with
provideHttpClient(withInterceptors([...]), withFetch())and functional interceptors in new code, notHttpClientModuleorHTTP_INTERCEPTORSclasses. Use interceptors only for cross-cutting concerns. - Avoid duplicate requests, cache only with a clear invalidation strategy, cancel requests the user no longer needs, and handle expected error states explicitly.
- Avoid manual URL string construction when typed helpers or centralized endpoints are available.
Errors
- Route unhandled errors to one
ErrorHandler;provideBrowserGlobalErrorListeners()forwards uncaught errors and unhandled promise rejections to it. Report them without secrets or personal data, and show users a recoverable state instead of a broken view.