diff --git a/package.json b/package.json index 45262dc..a6bd8bf 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "url": "https://github.com/yanick/updux/issues" }, "homepage": "https://github.com/yanick/updux#readme", - "types": "./types/index.d.ts", + "types": "./dist/index.d.ts", "prettier": { "tabWidth": 4, "singleQuote": true, diff --git a/src/Updux.ts b/src/Updux.ts index 76e32e2..fbb65d3 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -152,10 +152,7 @@ export class Updux { return this.#memoInitial(this.#initial, this.#subduxes); } - /** - * @return {Record} - */ - get actions() { + get actions(): Record { return this.#memoActions(this.#actions, this.#subduxes); } @@ -183,7 +180,7 @@ export class Updux { this.#reactions = [...this.#reactions, subscription]; } - setAction(type, payloadFunc) { + setAction(type, payloadFunc?: Function) { const theAction = action(type, payloadFunc); this.#actions = { ...this.#actions, [type]: theAction }; @@ -325,10 +322,10 @@ export class Updux { }; } - createStore(initial) { + createStore(initial?: unknown) { const store : { - getState: Function, - dispatch: Function, + getState: Function & Record, + dispatch: Function & Record, selectors: Record, actions: Record, } = reduxCreateStore( diff --git a/src/actions.js b/src/actions.js deleted file mode 100644 index ab7b7c7..0000000 --- a/src/actions.js +++ /dev/null @@ -1,17 +0,0 @@ -export function action(type, payloadFunction = null) { - const generator = function (payloadArg) { - const result = { type }; - - if (payloadFunction) { - result.payload = payloadFunction(payloadArg); - } else { - if (payloadArg !== undefined) result.payload = payloadArg; - } - - return result; - }; - - generator.type = type; - - return generator; -} diff --git a/src/actions.ts b/src/actions.ts new file mode 100644 index 0000000..c265700 --- /dev/null +++ b/src/actions.ts @@ -0,0 +1,39 @@ +export type Action = { + type: T; meta?: Record; } & ( + { payload?: TPayload } +) + +export type ActionGenerator = { + type: TType; +} & (TPayloadGen extends (...args:any) => any + ? (...args: Parameters) => { + type: TType; + payload: ReturnType; + } + : (...args: any[]) => { type: TType; payload?: unknown }); + +// interface Action { +// (actionType: T): ActionGenerator; +// ( +// actionType: T, +// payloadGen: F +// ): ActionGenerator; +// } + +export function action(type, payloadFunction = null) { + const generator = function (...payloadArg) { + const result :Action = { type }; + + if (payloadFunction) { + result.payload = payloadFunction(...payloadArg); + } else { + if (payloadArg[0] !== undefined) result.payload = payloadArg[0]; + } + + return result; + }; + + generator.type = type; + + return generator; +} diff --git a/src/buildSelectors/index.ts b/src/buildSelectors/index.ts index 2949a23..82c0904 100644 --- a/src/buildSelectors/index.ts +++ b/src/buildSelectors/index.ts @@ -5,7 +5,7 @@ export function buildSelectors(localSelectors, splatSelector = {}, subduxes ={}) if (!selectors) return {}; if (slice === '*') return {}; - return mapValues(selectors, (func) => (state) => func(state[slice])); + return mapValues(selectors, (func: Function) => (state) => func(state[slice])); }); let splat = {}; diff --git a/src/buildUpreducer.js b/src/buildUpreducer.js index ba53fba..6d6bb54 100644 --- a/src/buildUpreducer.js +++ b/src/buildUpreducer.js @@ -8,6 +8,8 @@ export function buildUpreducer(initial, mutations, subduxes = {}) { : null; return (action) => (state) => { + if( !action?.type ) throw new Error("upreducer called with a bad action"); + let newState = state ?? initial; if (subReducers) {