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): patch state methods do not correctly resolve generic co… #4646

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions modules/signals/entities/spec/types/models.types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expecter } from 'ts-snippet';
import { compilerOptions } from './helpers';

describe('models', () => {
const expectSnippet = expecter(
(code) => `
import { patchState, signalStore, type, withMethods } from '@ngrx/signals';
import { addEntity, entityConfig, withEntities } from '@ngrx/signals/entities';
type User = { id: number; name: string };

${code}
`,
compilerOptions()
);

it('succeeds when entity collection name is passed as the type parameter', () => {
const snippet = `
const storeFactory = <Collection extends string>(
collection: Collection
) => {
const Store = signalStore(
withEntities({ entity: type<User>(), collection: collection }),
withMethods((store) => ({
addUsers(user: User): void {
patchState(store, addEntity(user, { collection: collection }));
},
}))
);

return new Store();
};
`;

expectSnippet(snippet).toSucceed();
});
});
11 changes: 8 additions & 3 deletions modules/signals/entities/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Signal } from '@angular/core';
import { Prettify } from '@ngrx/signals';

export type EntityId = string | number;

Expand All @@ -9,9 +10,13 @@ export type EntityState<Entity> = {
ids: EntityId[];
};

export type NamedEntityState<Entity, Collection extends string> = {
[K in keyof EntityState<Entity> as `${Collection}${Capitalize<K>}`]: EntityState<Entity>[K];
};
export type NamedEntityState<Entity, Collection extends string> = Prettify<
{
[K in `${Collection}EntityMap`]: EntityMap<Entity>;
} & {
[K in `${Collection}Ids`]: EntityId[];
}
>;
Comment on lines +13 to +19
Copy link
Member

@timdeschryver timdeschryver Jan 20, 2025

Choose a reason for hiding this comment

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

I'm not a big fan of this approach because we will have to keep this in sync with the EntityMap entity. To be honest, I'm also not sure why this works and adding Prettify around the current type doesn't.

Another solution is to change the signature of PartialStateUpdater, and wrap the State with Prettify:

export type PartialStateUpdater<State extends object> = (
  state: Prettify<State>
) => Partial<State>;


export type EntityProps<Entity> = {
entities: Signal<Entity[]>;
Expand Down