Skip to content

Patch methods do not correctly resolve generic collection names withEntities #4638

Open
@ivashikhmin

Description

@ivashikhmin

Which @ngrx/* package(s) are the source of the bug?

signals

Minimal reproduction of the bug/regression with instructions

A TypeScript error occurs when attempting to use the patchState function with addEntity on a store with withEntities. The error indicates a type mismatch between PartialStateUpdater<NamedEntityState<E, Collection>> and the expected parameter type.

import {
  patchState,
  signalStoreFeature,
  type,
  withMethods,
} from '@ngrx/signals';
import { addEntity, EntityId, withEntities } from '@ngrx/signals/entities';

function withFeature<E extends { id: EntityId }, Collection extends string>(
  collection: Collection
  // collection: string,  //this removes the errors
) {
  const config = {
    collection: collection,
    entity: type<E>(),
  };
  return signalStoreFeature(
    withEntities(config),
    withMethods((store) => ({
      add: (entity: E) =>
        patchState(
          store,
          addEntity(entity, { collection })
        ),
    }))
  );
}
X [ERROR] TS2345: Argument of type 'PartialStateUpdater<NamedEntityState<E, Collection>>' is not assignable to parameter of type 'Partial<{ [
K in keyof { [K in keyof NamedEntityState<E, Collection>]: NamedEntityState<E, Collection>[K]; }]: { [K in keyof NamedEntityState<E, Collection>]: NamedEntityState<...>[K]; }[K]; }> | PartialStateUpdater<...>'.
  Type 'PartialStateUpdater<NamedEntityState<E, Collection>>' is not assignable to type 'PartialStateUpdater<{ [K in keyof { [K in keyof Name
dEntityState<E, Collection>]: NamedEntityState<E, Collection>[K]; }]: { [K in keyof NamedEntityState<E, Collection>]: NamedEntityState<...>[K]; }[K]; }>'.
    Types of parameters 'state' and 'state' are incompatible.
      Type '{ [K in keyof { [K in keyof NamedEntityState<E, Collection>]: NamedEntityState<E, Collection>[K]; }]: { [K in keyof NamedEntityState<E, Collection>]: NamedEntityState<...>[K]; }[K]; }' is not assignable to type 'NamedEntityState<E, Collection>'.
        Type 'string' is not assignable to type 'keyof NamedEntityState<E, Collection>'.
          Type 'string' is not assignable to type 'keyof NamedEntityState<E, Collection>'.
            Type '"entityMap"' is not assignable to type '`${Collection}${Capitalize<K>}` | `${Collection}EntityMap` | `${Collection}Ids`'. [plugin angular-compiler]

https://stackblitz.com/edit/vitejs-vite-nuijcczs?file=src%2Fmain.ts

Expected behavior

Collection extends string generic could be used to make this feature type-safe and allow method naming like in
ngrx-toolkit withDataService

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

"@ngrx/signals": "^19.0.0",

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions