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>
3.6 KiB
3.6 KiB
Angular Components, Change Detection, and Signals
Apply these rules when creating or changing Angular components, directives, signal state, change detection, or DOM access.
Components
- Prefer standalone components, directives, and pipes for new work. Avoid introducing
NgModuleunless the existing architecture requires it. - Keep components small; move substantial business logic to a focused service or domain layer.
- Use
ChangeDetectionStrategy.OnPushunless there is a concrete reason not to, in both zone.js and zoneless applications. - Keep component state minimal, update it immutably, and never mutate shared state directly.
- In new code on Angular 19+, use
input()andinput.required(),output()(anOutputEmitterRef, not a signal),model()for intended two-way bindings, and signal queries (viewChild,viewChildren,contentChild,contentChildren). Keep@Input,@Output, and decorator queries only where the surrounding code uses them. - Use the
hostobject in component metadata instead of@HostBindingand@HostListenerin new code. - Prefer explicit data flow: inputs down, events up. Avoid unnecessary two-way binding, component coupling, and exposed internal state.
- Use lifecycle hooks only when required, and keep constructors free of complex initialization.
Change Detection
- Check whether the application uses zone.js or is zoneless: new applications are zoneless by default since Angular 21 (
provideZonelessChangeDetection, stable since 20.2). Do not migrate between the two as a side effect of another task. - In zoneless applications, change template-bound state only through signals,
markForCheck, theasyncpipe, or template event handlers. Mutating plain fields insetTimeoutor promise callbacks does not update the view. - Do not add
NgZoneAPIs (onStable,onMicrotaskEmpty,runOutsideAngular) for rendering logic; useafterNextRenderorafterRenderEffectfor DOM work after rendering.
Signals and State
- Prefer signals for local and synchronous reactive state.
- Derive values with
computedinstead of storing or duplicating them; keepcomputedfree of side effects. - Keep signals private when external mutation is not intended, and expose readonly state where practical.
- Avoid circular signal dependencies.
- Use
effectonly for actual side effects, not as a replacement for normal data flow. Keep effects small and deterministic, and clean up resources they create. - Use
linkedSignal(Angular 20+) for writable state that resets when a source changes, instead of aneffectthat writes signals. - For parameter-driven reads, use
httpResource,resource, orrxResource(stable since Angular 22; experimental in 19 to 21, so use them there only if the project already does). Use RxJS for event streams, cancellation semantics, and multi-step flows; convert withtoSignalat the component edge, in an injection context. Never perform writes or mutations through resources. - Do not use global state unless multiple unrelated parts of the application genuinely need it.
- Do not introduce a state-management library without a clear requirement.
DOM and Browser APIs
- Prefer Angular APIs and declarative templates over direct DOM manipulation; use
Renderer2only when a rendering abstraction is actually needed. - When the application uses SSR, guard
window,document,localStorage, and similar browser-only APIs withisPlatformBrowser, or run DOM access inafterNextRenderorafterRenderEffect, which run only in the browser. - Clean up event listeners, observers, and timers, and avoid layout thrashing in performance-sensitive code.