An `InjectionToken<T>` carries its type parameter all the way through `inject()`, so `useFeatureFlags()` returns a fully-typed `FeatureFlags` object with no casting anywhere in the chain. The `factory` option lets the token supply its own default when nothing else provides it, and because that factory itself calls `inject(RAW_CONFIG, { optional: true })`, the token can depend on another optional token without forcing every consumer to configure both. This pattern — a typed token whose factory composes other optional tokens — replaces the classic `@Optional() @Inject(TOKEN)` constructor boilerplate with a single declaration point that stays type-safe under strict mode.
import { InjectionToken, inject } from '@angular/core';interface FeatureFlags {readonly darkMode: boolean;readonly betaCheckout: boolean;}const RAW_CONFIG = new InjectionToken<Partial<FeatureFlags>>('RAW_CONFIG');const FEATURE_FLAGS = new InjectionToken<FeatureFlags>('FEATURE_FLAGS', {providedIn: 'root',factory: () => {const raw = inject(RAW_CONFIG, { optional: true });return {darkMode: raw?.darkMode ?? false,betaCheckout: raw?.betaCheckout ?? false,};},});function useFeatureFlags(): FeatureFlags {return inject(FEATURE_FLAGS);}