27 lines
806 B
TypeScript
27 lines
806 B
TypeScript
import u from '@yanick/updeep-remeda';
|
|
import * as R from 'remeda';
|
|
|
|
type SubduxState<S> = 'initialState' extends keyof S ? S['initialState'] : {};
|
|
|
|
export type AggregateState<LOCAL, SUBDUXES extends Record<any, any>> = LOCAL &
|
|
(keyof SUBDUXES extends never
|
|
? {}
|
|
: {
|
|
[Slice in keyof SUBDUXES]: Slice extends string
|
|
? SubduxState<SUBDUXES[Slice]>
|
|
: never;
|
|
});
|
|
|
|
export function buildInitial(localInitial, subduxes) {
|
|
if (Object.keys(subduxes).length > 0 && typeof localInitial !== 'object') {
|
|
throw new Error(
|
|
"can't have subduxes when the initialState value is not an object",
|
|
);
|
|
}
|
|
|
|
return u(
|
|
localInitial,
|
|
R.mapValues(subduxes, R.pathOr(['initialState'], {})),
|
|
);
|
|
}
|