timdeschryver / ngrx-immer

Immer wrappers around NgRx methods createReducer, on, and ComponentStore
https://www.npmjs.com/package/ngrx-immer
MIT License
115 stars 7 forks source link

ImmerComponentStore: defaultState (via constructor) is not immutable #26

Open SebastianPost1996 opened 1 month ago

SebastianPost1996 commented 1 month ago

When initializing a store and defining the initial state in the constructor, that initial store is not produced by immer and thus not immutable until a state change has been performed.

interface TestState {
  test: string;
}

@Injectable({ providedIn: 'root' })
export class TestStore extends ImmerComponentStore<TestState> {
  constructor() {
    super({ test: 'foo' });
    this.get().test = 'bar'; // should throw an error but does not
  }
}

Replacing the defaultState in the super call by a produced value correctly causes an error to be thrown.

super(produce({ test: 'foo' }, x => x));
timdeschryver commented 1 month ago

Thanks for raising this issue, do you want to create a PR for this @SebastianPost1996 ?

SebastianPost1996 commented 1 month ago

Added PR: https://github.com/timdeschryver/ngrx-immer/pull/27