Sub topic 7
Reading nested state with deep signals
This chapter assumes you know how withState, patchState and withComputed work from the earlier chapters. It also assumes you are comfortable with Angular signals.
The problem with nested state
Real state is rarely flat. A settings screen might hold a user, the user's address and a theme. If the store exposed only one signal per top-level key, a template that needs the city would have to read the whole user object. It would then reach into it with store.user().address.city.
SignalStore avoids this with deep signals. A deep signal is a signal that also exposes one child signal for each property of its object value.
Creating nested state
import { computed } from '@angular/core';
import { patchState, signalStore, withComputed, withMethods, withState } from '@ngrx/signals';
type SettingsState = {
user: { name: string; address: { city: string; country: string } };
theme: { dark: boolean };
tags: string[];
};
const initialState: SettingsState = {
user: { name: 'Ada', address: { city: 'London', country: 'UK' } },
theme: { dark: false },
tags: ['admin'],
};
export const SettingsStore = signalStore(
{ providedIn: 'root' },
withState(initialState),
withComputed(({ user }) => ({
label: computed(() => `${user.name()} from ${user.address.city()}`),
})),
withMethods((store) => ({
moveTo(city: string): void {
patchState(store, (state) => ({
user: { ...state.user, address: { ...state.user.address, city } },
}));
},
toggleDark(): void {
patchState(store, (state) => ({ theme: { dark: !state.theme.dark } }));
},
})),
);withState turns every top-level key into a signal. Because user holds an object, store.user is a deep signal. You can call it as store.user() to get the whole object. You can also read store.user.name() and store.user.address.city(). Each of those is a normal signal. The same property access works inside withComputed, as the label signal shows.
How it works
The child signals are created lazily, the first time you access them, and then cached. Each one is derived from its parent. It changes only when its own value changes, and the comparison uses reference equality. A component that reads store.user.address.city() is not notified when theme.dark changes. It is also not notified when user.name changes, as long as address keeps the same reference.
Using deep signals in a component
import { Component, inject } from '@angular/core';
import { SettingsStore } from './settings.store';
@Component({
selector: 'app-settings',
template: `
<p>{{ store.label() }}</p>
<p>Country: {{ store.user.address.country() }}</p>
<button (click)="store.moveTo('Paris')">Move to Paris</button>
<button (click)="store.toggleDark()">
Dark mode: {{ store.theme.dark() }}
</button>
`,
})
export class SettingsComponent {
readonly store = inject(SettingsStore);
}Where deep signals stop
Deep signals only appear for plain objects whose type is known to be a record. Three cases get an ordinary Signal instead:
- Arrays.
store.tagsis aSignal<string[]>. Writingstore.tags[0]does not work. Usestore.tags()[0]or a computed signal. - Primitives such as strings, numbers and booleans. These are leaves, so you call them directly.
- Nullable or union types, such as
address: Address | null. TypeScript cannot know the value is an object, so the property is a plain signal. Read it withstore.address()?.city.
Updating is still whole-object
Deep signals are read-only. There is no store.user.address.city.set(...). Every change goes through patchState, and you must still build new objects along the path you change. Spread each level, as moveTo does. If you mutate state.user.address.city in place, the reference does not change. The child signals will not notify, so the view may not update.
Common errors
- Reading
store.user.namein a template without calling it. The result is a signal function, not a string. - Expecting deep signals on array items or nullable objects.
- Copying only the top-level object and mutating a nested one.
Exercise
A store has state { profile: { email: string; prefs: { lang: string } } | null; items: { id: number }[] }. Which of store.profile, store.items and store.profile.prefs can you read as a deep signal, and why?
Show answer
None of them. profile is typed as {...} | null, which is not a known record, so store.profile is a plain Signal and has no child signals. store.profile.prefs therefore does not exist. store.items is an array, so it is also a plain signal. Read them with store.profile()?.prefs.lang and store.items(). If profile were non-nullable, store.profile.prefs.lang() would work.