-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: main
Are you sure you want to change the base?
fix(signals): enable withProps
to handle Symbols
#4656
Conversation
Resolve an issue in `signalStore` that prevented Symbols from being added as property keys when merging different features.
✅ Deploy Preview for ngrx-io canceled.Built without sensitive environment variables
|
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'); | ||
}); |
There was a problem hiding this comment.
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
usesObject.getOwnPropertyNames
. - Assertion for duplicate keys defined as symbols. Currently, it will not work because
Object.keys
is used with theassertUniqueStoreMembers
function.
if (typeof key === 'string' || typeof key === 'symbol') { | ||
(this as any)[key] = storeMembers[key]; | ||
} |
There was a problem hiding this comment.
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?
if (typeof key === 'string' || typeof key === 'symbol') { | |
(this as any)[key] = storeMembers[key]; | |
} | |
(this as any)[key] = storeMembers[key]; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
Resolve an issue in
signalStore
that prevented Symbols from being added as property keys when mergingdifferent features.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
The value is just undefined.
Closes #4655
Does this PR introduce a breaking change?