ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.05k stars 1.98k forks source link

RFC(@ngrx/signals): Add type of id as optional second type arg to ` withEntities<Entity,Id>()` #4570

Open ptandler opened 1 month ago

ptandler commented 1 month ago

Which @ngrx/* package(s) are relevant/related to the feature request?

signals

Information

The withEntities feature uses EntityId for the entities' id, which is defined as export type EntityId = string | number.

We have a use case where we need to narrow down the type of the id to (e.g.) UUID (or some other sub-type of string).

So I'd suggest adding a second optional type arg to withEntities() like this:

export declare function withEntities<Entity, Id extends EntityId = EntityId>(): SignalStoreFeature<EmptyFeatureResult, {
    state: EntityState<Entity, Id>;
    computed: EntityComputed<Entity, Id>;
    methods: {};
}>;

Describe any alternatives/workarounds you're currently using

At the moment we need to cast the id from EntityId to UUID:

signalStore(
    withEntities<V>(),
    withMethods((store) => ({
      allIds(): K[] {
        return store.ids() as K[];
      },
//...
)

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