diff --git a/src/Updux.ts b/src/Updux.ts index 2352b17..1bc0d37 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -18,23 +18,10 @@ import { import { withPayload } from './actions.js'; import { AggregateActions, Dux, UnionToIntersection } from './types.js'; import { buildActions } from './buildActions.js'; -import { buildInitial } from './buildInitial.js'; - -type SubduxState = 'initial' extends keyof S ? S['initial'] : {}; - -type AggregateState> = LOCAL & - (keyof SUBDUXES extends never - ? {} - : { - [Slice in keyof SUBDUXES]: Slice extends string - ? SubduxState - : never; - }); +import { buildInitial, AggregateState } from './initial.js'; type MyActionCreator = { type: string } & ((...args: any) => any); -type F = null extends any ? 'u' : 'n'; - type ResolveAction< ActionType extends string, ActionArg extends any, @@ -69,33 +56,33 @@ export default class Updux< T_LocalActions extends { [actionType: string]: any; } = {}, - SUBDUXES extends Record = {}, + T_Subduxes extends Record = {}, > { #localInitial: T_LocalState; #localActions: T_LocalActions; #localMutations: Record< string, - Mutation, AggregateState> + Mutation, AggregateState> > = {}; - #subduxes: SUBDUXES; + #subduxes: T_Subduxes; #name: string; - #actions: AggregateActions, SUBDUXES>; + #actions: AggregateActions, T_Subduxes>; - #initial: AggregateState; + #initial: AggregateState; constructor( config: Partial<{ initial: T_LocalState; actions: T_LocalActions; - subduxes: SUBDUXES; + subduxes: T_Subduxes; }>, ) { // TODO check that we can't alter the initial after the fact this.#localInitial = config.initial ?? ({} as T_LocalState); this.#localActions = config.actions ?? ({} as T_LocalActions); - this.#subduxes = config.subduxes ?? ({} as SUBDUXES); + this.#subduxes = config.subduxes ?? ({} as T_Subduxes); this.#actions = buildActions(this.#localActions, this.#subduxes); @@ -128,7 +115,7 @@ export default class Updux< // TODO force the actionCreator to be one of the actions? mutation>( actionCreator: A, - mutation: Mutation>, + mutation: Mutation>, ) { this.#localMutations[(actionCreator as any).type] = mutation; } diff --git a/src/buildInitial.test.ts b/src/buildInitial.test.ts index 7e8767e..63743e8 100644 --- a/src/buildInitial.test.ts +++ b/src/buildInitial.test.ts @@ -1,5 +1,5 @@ import { test, expect } from 'vitest'; -import { buildInitial } from './buildInitial.js'; +import { buildInitial } from './initial.js'; test('basic', () => { expect( diff --git a/src/buildInitial.ts b/src/initial.ts similarity index 52% rename from src/buildInitial.ts rename to src/initial.ts index 7e2330e..36aaf92 100644 --- a/src/buildInitial.ts +++ b/src/initial.ts @@ -1,6 +1,17 @@ import u from '@yanick/updeep-remeda'; import * as R from 'remeda'; +type SubduxState = 'initial' extends keyof S ? S['initial'] : {}; + +export type AggregateState> = LOCAL & + (keyof SUBDUXES extends never + ? {} + : { + [Slice in keyof SUBDUXES]: Slice extends string + ? SubduxState + : never; + }); + export function buildInitial(localInitial, subduxes) { if (Object.keys(subduxes).length > 0 && typeof localInitial !== 'object') { throw new Error(