Sub topic 4
Handling the first value: initialValue and requireSync
A signal always has a value. An observable might not have emitted anything yet. This chapter covers how toSignal bridges that gap with initialValue and requireSync, and when to pick each one.
This chapter assumes you know how to create and read signals, you know basic RxJS (Observable, subscribe, map), and you can read TypeScript generics.
The problem: a signal needs a value now
When you call toSignal(source$), Angular subscribes right away. If the observable has not emitted yet, the signal still has to return something when you read it. By default, toSignal returns undefined in that case, and it adds undefined to the signal's type.
import { toSignal } from '@angular/core/rxjs-interop';
import { Observable } from 'rxjs';
declare const source$: Observable<number>;
// Type: Signal<number | undefined>
const value = toSignal(source$);Every consumer must now handle undefined, even if the value arrives a few milliseconds later. The two options below remove undefined from the type, and they work in different ways.
Option 1: initialValue
initialValue supplies a value for the signal to hold until the observable emits. You choose that value yourself.
import { Component } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { interval } from 'rxjs';
@Component({
selector: 'app-ticker',
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class Ticker {
// Signal<number>: initialValue removes undefined from the type.
seconds = toSignal(interval(1000), { initialValue: 0 });
}interval(1000) emits nothing for the first second. Without initialValue, the template would show an empty value for that second. With it, the template shows 0 immediately and then updates as the interval emits.
Use initialValue when the observable is asynchronous, such as an HTTP request, a timer, or a WebSocket. The initial value acts as a placeholder: an empty array, 0, null, or a loading string.
Option 2: requireSync
Some observables emit synchronously during subscribe. A BehaviorSubject is the common example, because it always replays its current value to new subscribers. For these, requireSync: true tells toSignal to expect a value immediately.
import { Component } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'app-counter',
template: `<p>{{ count() }}</p>`,
})
export class Counter {
private count$ = new BehaviorSubject(0);
// Signal<number>: requireSync is valid because BehaviorSubject emits synchronously on subscribe.
count = toSignal(this.count$, { requireSync: true });
}The type is Signal<number>, and no placeholder value is needed because the real value is available at once.
requireSync is a promise you make to Angular. If the observable does not emit synchronously, toSignal throws an error instead of falling back to undefined. That makes the mistake visible early.
Choosing between them
| Situation | Option | Resulting type | Column |
|---|---|---|---|
| Source is asynchronous (HTTP, timer) | `initialValue` | `Signal<T>` | |
| Source emits synchronously (`BehaviorSubject`, `of(...)`) | `requireSync: true` | `Signal<T>` | |
| Missing value is meaningful | neither | `Signal<T \ | undefined>` |
You cannot pass both options together. They answer the same question in two different ways: initialValue says "use this until something arrives", and requireSync says "something will already be there".
Leaving both out is a legitimate choice. If "no value yet" is a state your UI should handle explicitly, the undefined in the type forces every reader to deal with it.
Common mistakes
- Using
requireSyncwith an HTTP call.HttpClientrequests are asynchronous, sotoSignalthrows. UseinitialValueinstead. - Choosing an
initialValuethat looks like real data. An empty list and "no results found" look the same to the user. If the distinction matters, useundefinedor a separate loading flag. - Assuming
initialValueis replayed later. It is used only until the first emission. After that, the signal tracks the observable.
Exercise
You write toSignal(this.http.get<string[]>('/api/items'), { requireSync: true }). What happens, and what is the fix?
Show answer
HttpClient emits asynchronously, so no value is available at subscription time and toSignal throws an error. Replace requireSync: true with initialValue: [] as string[], which gives a Signal<string[]> that starts empty and updates when the response arrives.
Exercise
What is the type of toSignal(of('a')), and what option would change it to Signal<string> without a placeholder value?
Show answer
The type is Signal<string | undefined>, because toSignal does not check whether the source is synchronous unless you say so. Adding requireSync: true gives Signal<string>, which is valid because of('a') emits synchronously on subscribe.