Proper typing of unionEntitiesAsMap() #142
-
I would like to fix |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Try to use the idKey type directly |
Beta Was this translation helpful? Give feedback.
-
Ok, theoretically I understand what should be done, but still can't get exactly the right results. export function unionEntitiesAsMap<
T extends {
entities: Record<string, any>[];
UIEntities: Record<string | number, Record<string, any>>;
}
>(
idKey: keyof T['entities'][0] = 'id'
): OperatorFunction<T, Record<T['entities'][0][typeof idKey], T['entities'][0] & T['UIEntities'][0]>> {
return map((state: T) => {
return Object.fromEntries(
state.entities.map((entity) => {
return [
entity[idKey as string],
{
...entity,
...state.UIEntities[entity[idKey as string]],
},
];
})
);
});
} when used as follows: enum FooBar {A, B, C}
export interface Foo {
id: FooBar;
title: string;
completed: boolean;
}
export interface UIFoo {
id: FooBar;
open: boolean;
}
const { state, config } = createState(
withEntities<Foo>(),
withUIEntities<UIFoo>()
);
const store = new Store({ state, name: 'todos', config });
const fooMap$ = store
.combine({
entities: store.pipe(selectAll()),
UIEntities: store.pipe(selectEntities({ ref: UIEntitiesRef })),
})
.pipe(unionEntitiesAsMap('id')); doesn't give me the expecpected typing of What should I use instead of the following mess:
|
Beta Was this translation helpful? Give feedback.
Try to use the idKey type directly