← Back to Angular, page 1

angular-0020 · Medium mid

What is linkedSignal, and how is it different from computed()?

computed() is read-only and always follows its sources. linkedSignal() is also derived from a source, but you can write to it. When the source changes, it is recalculated, so it resets to a sensible default. It fits state such as a selected item that must reset when its list changes.

Full answer

A computed is perfect for values that are only derived: a total, a grade, a filtered list. You cannot set it.

Sometimes a value is derived by default but the user can override it. Example: a list of courses and a "selected course". The selection starts as the first course. The user can pick another one. When the list is replaced by a new list, the old selection may no longer exist, so the selection should reset.

Before linkedSignal this needed an effect that wrote to a normal signal, which is the pattern to avoid. linkedSignal solves it:

ts
selected = linkedSignal({
  source: this.courses,
  computation: (list, previous) =>
    previous && list.includes(previous.value) ? previous.value : list[0],
});

It behaves like a writable signal. selected.set('RxJS') works. When courses changes, the computation runs again with the new source value and the previous state, so you decide whether to keep the user's choice or reset it.

The short form linkedSignal(() => this.courses()[0]) recomputes whenever any signal it reads changes, and the user's own set is lost at that moment.

Both are lazy and cached. linkedSignal became stable in Angular 20. Some details that make the answer complete:

  • In the long form, the computation function receives the new source value and a previous object with source and value. In the example, this lets you keep the same selection if the item still exists in the new list.
  • The short form has no source. It re-runs whenever any signal that it read changes, and the writes the user made before are replaced.
  • A second example is pagination. A page linked to the filter signal goes back to 1 when the filter changes, but the user can still press "next" and set the page.
  • linkedSignal accepts an equal function, like computed, to avoid updates when the new value is considered equal.
  • Do not use an effect that sets another signal for this job. An effect runs later and can show an old value for one render. linkedSignal updates when it is read, so there is no stale value.
  • Choose computed when the value must never be changed by the user, linkedSignal when the value has a default from other state but can be edited, and a plain signal when the value does not depend on anything.
typescript
import { linkedSignal, signal } from '@angular/core';

const courses = signal(['Angular', 'RxJS', 'TypeScript']);

const selected = linkedSignal<string[], string>({
  source: courses,
  computation: (list, previous) =>
    previous && list.includes(previous.value) ? previous.value : list[0],
});

console.log(selected()); // Angular

selected.set('RxJS');
console.log(selected()); // RxJS

// New list still contains RxJS: the user's choice is kept.
courses.set(['RxJS', 'Signals']);
console.log(selected()); // RxJS

// New list no longer contains it: reset to the first item.
courses.set(['Testing', 'Routing']);
console.log(selected()); // Testing

Tested with Angular 20.3 under Node 22.18 or newer (`node main.ts`, package.json with `type: module`). Output is identical on repeated runs: `Angular`, `RxJS`, `RxJS` (kept), `Testing` (reset).

Key points

  • `computed` is read-only; `linkedSignal` is writable and derived.
  • The `source` plus `computation(source, previous)` form lets you keep the old value when it is still valid.
  • Setting a linked signal works until the source changes, then it recomputes.
  • Replaces the "effect that sets another signal" pattern.

Common mistakes

  • Using an effect to reset a selection when data changes.
  • Using `computed` and then trying to call `set` on it.
  • Ignoring `previous` and always resetting, which throws away the user's valid choice.

Follow-ups

  • What is the type of `previous` in the computation function?
  • How is this different from a `model()` input?
  • Can you give a form-related example?

Tips

  • Use the selected-item example. It is the clearest story.
  • Say "derived but overridable" in one sentence. It is a good summary.
  • Mention that you replaced an effect with it, and why that is safer.

Evaluation rubric

  • Distinguishes read-only from writable derived state.
  • Writes a correct linkedSignal with previous.
  • Explains when it resets.