Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(signals): enable withProps to handle Symbols #4656

Merged
Merged
23 changes: 22 additions & 1 deletion modules/signals/spec/with-props.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { signal } from '@angular/core';
import { of } from 'rxjs';
import { withMethods, withProps, withState } from '../src';
import { signalStore, withMethods, withProps, withState } from '../src';
import { getInitialInnerStore } from '../src/signal-store';
import { TestBed } from '@angular/core/testing';

describe('withProps', () => {
it('adds properties to the store immutably', () => {
Expand Down Expand Up @@ -47,4 +48,24 @@ describe('withProps', () => {
's1, p2, m1'
);
});

it('allows symbols as props', () => {
const SECRET = Symbol('SECRET');

const Store = signalStore(withProps(() => ({ [SECRET]: 'secret' })));
const store = TestBed.configureTestingModule({ providers: [Store] }).inject(
Store
);

expect(store[SECRET]).toBe('secret');
});

it('allows numbers as props', () => {
const Store = signalStore(withProps(() => ({ 1: 'Number One' })));
const store = TestBed.configureTestingModule({ providers: [Store] }).inject(
Store
);

expect(store[1]).toBe('Number One');
});
rainerhahnekamp marked this conversation as resolved.
Show resolved Hide resolved
});
12 changes: 9 additions & 3 deletions modules/signals/src/signal-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,12 +1355,18 @@ export function signalStore(
getInitialInnerStore()
);
const { stateSignals, props, methods, hooks } = innerStore;
const storeMembers = { ...stateSignals, ...props, ...methods };
const storeMembers: Record<string | symbol, unknown> = {
...stateSignals,
...props,
...methods,
};

(this as any)[STATE_SOURCE] = innerStore[STATE_SOURCE];

for (const key in storeMembers) {
(this as any)[key] = storeMembers[key];
for (const key of Reflect.ownKeys(storeMembers)) {
if (typeof key === 'string' || typeof key === 'symbol') {
rainerhahnekamp marked this conversation as resolved.
Show resolved Hide resolved
(this as any)[key] = storeMembers[key];
}
rainerhahnekamp marked this conversation as resolved.
Show resolved Hide resolved
}

const { onInit, onDestroy } = hooks;
Expand Down
Loading