updux/src/types.ts

38 lines
1004 B
TypeScript
Raw Normal View History

2021-10-15 16:41:58 +00:00
export type Dict<T> = Record<string, T>;
2021-10-18 12:46:00 +00:00
2021-10-18 13:02:20 +00:00
export type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never;
2021-10-18 12:46:00 +00:00
type Subdux<TState = any> = {
2021-10-18 13:02:20 +00:00
initial: TState;
2021-10-18 12:46:00 +00:00
};
type StateOf<D> = D extends { initial: infer I } ? I : unknown;
2021-10-18 14:15:06 +00:00
type ActionsOf<C> = C extends { actions: infer A }
? {
[K in keyof A]: Function;
}
: {};
2021-10-18 12:46:00 +00:00
2021-10-18 13:02:20 +00:00
type Subduxes = Record<string, Subdux>;
2021-10-18 12:46:00 +00:00
export type DuxStateSubduxes<C> = C extends { '*': infer I }
? {
[key: string]: StateOf<I>;
[index: number]: StateOf<I>;
}
2021-10-18 13:02:20 +00:00
: { [K in keyof C]: StateOf<C[K]> };
export type AggregateDuxState<TState, TSubduxes> = TState &
DuxStateSubduxes<TSubduxes>;
type DuxActionsSubduxes<C> = C extends object ? ActionsOf<C[keyof C]> : unknown;
2021-10-18 12:46:00 +00:00
2021-10-18 13:02:20 +00:00
type ItemsOf<C> = C extends object ? C[keyof C] : unknown;
2021-10-18 12:46:00 +00:00
2021-10-18 13:02:20 +00:00
export type AggregateDuxActions<TActions, TSubduxes> = TActions &
UnionToIntersection<ActionsOf<ItemsOf<TSubduxes>>>;