NG.II · Angular 22 practice question
A developer is managing an `Order` object within a parent signal. They use `linkedSignal` to create a derived signal for the `shippingMethod`. If they call `shippingMethod.set('Overnight')`, what must the implementation of the `set` function ensure to correctly update the parent signal?
interface Order {
id: number;
shippingMethod: string;
}
const order = signal<Order>({ id: 42, shippingMethod: 'Ground' });
const shippingMethod = linkedSignal(() => order().shippingMethod, {
set: (newMethod) => {
// Implementation goes here
},
});
Check answer and explanation
Correct answer: It must use `order.update((currentOrder) => ({ ...currentOrder, shippingMethod: newMethod }))` to create an immutable copy.
Explanation
The recommended pattern involves employing `order.update` alongside the spread operator to guarantee that the parent object is modified in an immutable fashion.
Common trap
Directly altering the existing object reference violates the tenets of signal state management, and replacing the entire object is less targeted than modifying only the required field.
Source: Angular docs: Dependent state with `linkedSignal` — Updating a property inside a parent object
Checked against the cited text on 2026-11-25.