Skip to content

Signal Store Allows Writing to Read-Only Signals #4958

@manfredsteyer

Description

@manfredsteyer

Which @ngrx/* package(s) are the source of the bug?

signals

Minimal reproduction of the bug/regression with instructions

By default, the Signal Store returns signals of type Signal<...> (and not WriteableSignal<...>). However, in the current version, it's possible to write into these Signals. I'm pretty sure this was not possible in former versions.

Here is a reproduction:

import { Component, inject, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { signalStore, withState } from '@ngrx/signals';

const DemoStore = signalStore(
  {providedIn: 'root'},
  withState({
    title: 'Test!',
  }),
);

@Component({
  selector: 'app-root',
  imports: [FormsModule],
  template: `
    <h1>Reproduction</h1>
    <input [(ngModel)]="title">
    <div>{{title()}}</div>
  `,
  styleUrl: './app.css'
})
export class App {
  // Here, everything is fine:
  // protected readonly title = signal('test');

  // This option allows the two-way-binding to directly
  // write back to the signal in the store ...
  store = inject(DemoStore);
  title = this.store.title;

  // Writing back the value after casting to WritableSignal works too.
  constructor() {
    const title = this.title as WritableSignal<string>
    title.set('Hallo!!!!');
  }

}

Expected behavior

Writing back should not be possible.

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

@angular/[email protected]
@ngrx/[email protected]

Other information

We need to call asReadonly() i/o casting to Signal<...>.

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions