Question presented to candidate:
"A teammate has a BehaviorSubject in a service and a signal-based component, and keeps writing subscribe() calls to glue them together. Explain how toSignal() and toObservable() connect the two worlds, and what each one actually emits."
What a strong answer should cover:
- Both live in
@angular/core/rxjs-interopand are stable (the 22.2.3 typings mark them@publicApi 20.0). toSignal(obs$)subscribes immediately, stores each value in a signal, and unsubscribes when the injection context is destroyed. Before the first emission the value isundefinedunless you passinitialValueorrequireSync: true.- If the Observable errors, reading the signal throws that error; completion simply freezes the last value.
toObservable(sig)is implemented with an effect feeding aReplaySubject(1): it emits asynchronously, only the settled value (several synchronousset()calls become one emission), and late subscribers get the latest value.- Both need an injection context (or an
injectoroption);toSignal()must not be called insidecomputed()/effect()(NG0602). - Typical uses:
toSignal(http$)ortoSignal(route.paramMap)for templates;toObservable(query)to applydebounceTime/switchMap.
Clarifying questions expected:
- "Does the service need to stay Observable-based for other consumers, or can it expose a signal?"
- "Does the template need a value before the first emission?"
Code / implementation expected: Yes β an interval turned into a signal, and a signal turned into an Observable that shows which values it really emits.