Skip to content

Commit c5a5a27

Browse files
fix(signals): add unique member check to withLinkedState (#4932)
Closes #4931
1 parent ca48268 commit c5a5a27

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

modules/signals/spec/with-linked-state.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,23 @@ describe('withLinkedState', () => {
235235
expect(name()).toBe('Mark');
236236
});
237237
});
238+
239+
it('logs a warning if previously defined signal store members have the same name', () => {
240+
vi.spyOn(console, 'warn').mockImplementation(() => {});
241+
242+
const linkedStateFeature = signalStoreFeature(
243+
withState({ value: 1 }),
244+
withLinkedState(() => ({
245+
value: () => 1,
246+
}))
247+
);
248+
249+
linkedStateFeature(getInitialInnerStore());
250+
251+
expect(console.warn).toHaveBeenCalledWith(
252+
'@ngrx/signals: SignalStore members cannot be overridden.',
253+
'Trying to override:',
254+
'value'
255+
);
256+
});
238257
});

modules/signals/src/with-linked-state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { linkedSignal, WritableSignal } from '@angular/core';
22
import { toDeepSignal } from './deep-signal';
3+
import { assertUniqueStoreMembers } from './signal-store-assertions';
34
import {
45
InnerSignalStore,
56
SignalsDictionary,
@@ -85,6 +86,7 @@ export function withLinkedState<
8586
...store.props,
8687
});
8788
const stateKeys = Reflect.ownKeys(linkedState);
89+
assertUniqueStoreMembers(store, stateKeys);
8890
const stateSource = store[STATE_SOURCE] as SignalsDictionary;
8991
const stateSignals = {} as SignalsDictionary;
9092

0 commit comments

Comments
 (0)