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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rainerhahnekamp
Copy link
Contributor

Resolve an issue in signalStore that prevented Symbols from being added as property keys when merging
different features.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

The value is just undefined.

Closes #4655

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Resolve an issue in `signalStore` that prevented Symbols
from being added as property keys when merging
different features.
Copy link

netlify bot commented Jan 3, 2025

Deploy Preview for ngrx-io canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 34fd125
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/677aaa9e292e3400080f0931

Comment on lines 52 to 70
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');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The with-props.spec.ts file should contain only unit tests for the withProps function. Check other tests from this file for more info.

These (integration) tests with SignalStore should be added to the signal-store.spec.ts: https://github.com/ngrx/platform/blob/main/modules/signals/spec/signal-store.spec.ts#L150


Let's also add a test that verifies that a symbol prop is available in the next feature, not only in the final store:

signalStore(
  withProps(() => ({ [symbol]: 1 })),
  withMethods((store) => store[symbol]...
)

We should also have tests to verify:

  • Behavior when state slice/method name is a symbol.
  • Immutability checks for symbol state slices. I don't think the current check will work - deepFreeze uses Object.getOwnPropertyNames.
  • Assertion for duplicate keys defined as symbols. Currently, it will not work because Object.keys is used with the assertUniqueStoreMembers function.

Comment on lines 1367 to 1369
if (typeof key === 'string' || typeof key === 'symbol') {
(this as any)[key] = storeMembers[key];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for adding this if? is there any other type of key other than string or symbol that should be ignored?

Suggested change
if (typeof key === 'string' || typeof key === 'symbol') {
(this as any)[key] = storeMembers[key];
}
(this as any)[key] = storeMembers[key];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key in this case is of type string | symbol and TypeScript doesn't allow an access with that type. That's why I had to apply type narrowing.

Litte bit strange, since I've never experienced that before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and it seems to work on my machine 😅

1

Anyway, instead of adding additional LOC that will end up in the final bundle unnecessarily in order to satisfy TS, I suggest using type casting if it doesn't work without if (typeof key === 'string' ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okayyy 🙃, looks like IntelliJ played a joke on me. I’ll go ahead and remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

withProps doesn't allow Symbols as keys
3 participants