Skip to content

Type of idKey is not recognized correctly when using generics #4217

@Raul52

Description

@Raul52

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

signals

Minimal reproduction of the bug/regression with instructions

I am trying to create a signal store feature using generics and I stumbled upon a typing error when trying to use the idKey:
https://ngrx.io/guide/signals/signal-store/entity-management#customized-id-property

I have the following feature:

export interface Entity {
    uuid: EntityId;
}

export function withEntityLoader<E extends Entity, F, S extends LoaderService<E, F>>(loaderServiceType: Type<S>, filter: F) {
    return signalStoreFeature(
    ......
        withMethods((store) => {
            const dataService = inject(loaderServiceType);

            return {
                load() {
                    rxMethod<void>(
                    ....................................................
                                    tapResponse({
                                        next: response => {
                                            patchState(store, setAllEntities(response.items, {idKey: 'uuid'}));
                                        },
                                    }),
                                ),
                            ),
                        ),
                    );
                },
            };
        }),
    );
}

I checked how the type definition is made internally and tried to reproduce it with as concise an example as I could.

context:

  • usingType() function is used to mimick what any set* method would do if you accessed the config object.

Current faulty behavior

Expected behavior

The expected behavior is that uuid is recognized as a key with the correct type.

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

"@ngrx/signals": "17.1.0"

Other information

If you change the type of EntityIdProps to

export type EntityIdProps<Entity> = {
    [K in keyof Entity as K extends EntityId ? K : never]: Entity[K];
};

Working playground

Then you will have the correct typing.

Seems like the type is not correctly resolved when using the indexed property accessor via generics which extends a known type.

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