From 64225c52059cea96e6c1ecfbb2e01a0d19b03f11 Mon Sep 17 00:00:00 2001 From: zcf0508 Date: Tue, 22 Aug 2023 13:29:43 +0800 Subject: [PATCH] chore: update comment --- src/index.ts | 111 +++++++++++++++++++++------------------------ test/index.test.ts | 1 + test/utils.ts | 3 -- 3 files changed, 53 insertions(+), 62 deletions(-) delete mode 100644 test/utils.ts diff --git a/src/index.ts b/src/index.ts index 6ee7438..457acda 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,18 +63,25 @@ export function defineModule< * @param payload Pass `undefined` when no parameters are required */ (state: STATE, payload: MUTATIONS[K]) => void } - /** not support `rootState` and `rootGetters` */ - actions?: { [K in keyof ACTIONS]: < - DISPATCH extends ModuleDispatch, - ACTIONGETTERS extends GETTERS, + actions?: { + /** + * @param ctx not support `rootState` and `rootGetters` + * @param payload Pass `undefined` when no parameters are required, and + */ + [K in keyof ACTIONS]: < + DISPATCH extends ModuleDispatch, + ACTIONGETTERS extends GETTERS, >(ctx: { state: STATE commit: ModuleCommit dispatch: DISPATCH getters: ACTIONGETTERS - }, payload: ACTIONS[K]) => any } - /** not support `rootState` and `rootGetters` */ - getters?: { [K in keyof GETTERS]: (state: STATE, getters: LGS) => GETTERS[K] } + }, payload: ACTIONS[K]) => any + } + getters?: { + /** not support `rootState` and `rootGetters` */ + [K in keyof GETTERS]: (state: STATE, getters: LGS) => GETTERS[K] + } // modules?: NS extends true // ? MDS extends Record> // ? MDS @@ -168,9 +175,10 @@ type StoreState = Readonly< > type StoreCommit = - & And<( + And<( MODULES[keyof MODULES] extends ModuleInstance - ? ({ < + ? ({ + < K extends keyof FlattenMutations< MODULES extends Modules ? MODULES : never >, @@ -194,72 +202,55 @@ type StoreCommit = >[1] : never): void }) : never - ), { - (type: T, payload: MUTATIONS[T]): void - (input: { type: T } & MUTATIONS[T]): void - }> + ), { + (type: T, payload: MUTATIONS[T]): void + (input: { type: T } & MUTATIONS[T]): void + }> type StoreDispatch = And<(MODULES[keyof MODULES] extends ModuleInstance ? { < K extends keyof FlattenActions< - MODULES extends Record | NSModule> - ? MODULES - : never + MODULES extends Modules ? MODULES : never >, >(type: K, payload: Parameters< FlattenActions< - MODULES extends Record | NSModule> - ? MODULES - : never + MODULES extends Modules ? MODULES : never >[K] extends (...args: any) => any ? FlattenActions< - MODULES extends Record | NSModule> - ? MODULES - : never + MODULES extends Modules ? MODULES : never >[K] : never >[1]): Promise < K extends keyof FlattenActions< - MODULES extends Record | NSModule> - ? MODULES - : never + MODULES extends Modules ? MODULES : never >, >(input: { type: K } & Parameters< FlattenActions< - MODULES extends Record | NSModule> + MODULES extends Modules ? MODULES : never >[K] extends (...args: any) => any ? FlattenActions< - MODULES extends Record | NSModule> + MODULES extends Modules ? MODULES : never >[K] : never >[1]): Promise } : never) - , { - (type: T, payload: ACTIONS[T]): Promise - (input: { type: T } & ACTIONS[T]): Promise - }> + , { + (type: T, payload: ACTIONS[T]): Promise + (input: { type: T } & ACTIONS[T]): Promise + }> type StoreGetters = - And<(MODULES[keyof MODULES] extends ( - | Module - | NSModule - ) ? { - [K in keyof FlattenGetters< - MODULES extends Record - | NSModule - )> ? MODULES : never - >]: - FlattenGetters< - MODULES extends Record - | NSModule - )> ? MODULES : never - >[K] - } + And<(MODULES[keyof MODULES] extends ModuleInstance ? { + [K in keyof FlattenGetters< + MODULES extends Modules ? MODULES : never + >]: + FlattenGetters< + MODULES extends Modules ? MODULES : never + >[K] + } : never), { [K in keyof GETTERS]: GETTERS[K] } > @@ -421,7 +412,7 @@ interface MapState { // --- interface StoreWrap< - /** modeuls */ MODULES, ROOTSTATE, MUTATIONS, ACTIONS, GETTERS, + MODULES, ROOTSTATE, MUTATIONS, ACTIONS, GETTERS, > { store: Omit>>, 'state' | 'commit' | 'dispatch' | 'getters'> & { state: StoreState @@ -438,11 +429,8 @@ interface StoreWrap< mapState: MapState } -/** -* 定义 Store 的方法 -*/ export function defineStore< - /** modeuls */ MODULES, ROOTSTATE extends Record, MUTATIONS, ACTIONS, GETTERS, + MODULES, ROOTSTATE extends Record, MUTATIONS, ACTIONS, GETTERS, >(options: { modules?: { [K in keyof MODULES]: MODULES[K] extends ModuleInstance @@ -452,18 +440,23 @@ export function defineStore< state?: ROOTSTATE mutations?: { [K in keyof MUTATIONS]: /** - * @param payload 不需要参数时请传 undefined + * @param payload Pass `undefined` when no parameters are required */ (state: ROOTSTATE, payload: MUTATIONS[K]) => void } - actions?: { [K in keyof ACTIONS]: < - DISPATCH extends StoreDispatch, - ACTIONGETTERS extends GETTERS, - >(ctx: { + actions?: { + /** + * @param payload Pass `undefined` when no parameters are required + */ + [K in keyof ACTIONS]: < + DISPATCH extends StoreDispatch, + ACTIONGETTERS extends GETTERS, + >(ctx: { state: StoreState commit: StoreCommit dispatch: DISPATCH getters: ACTIONGETTERS - }, payload: ACTIONS[K]) => void } + }, payload: ACTIONS[K]) => void + } getters?: { [K in keyof GETTERS]: (state: StoreState) => GETTERS[K] } }): StoreWrap { diff --git a/test/index.test.ts b/test/index.test.ts index 7b17436..62c632d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -295,6 +295,7 @@ describe('vuex', () => { }, }) + // vuex not support assertType(vm.uname) assertType(vm.newGUsername) assertType(vm.gUsername) diff --git a/test/utils.ts b/test/utils.ts deleted file mode 100644 index c313b59..0000000 --- a/test/utils.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function expectType(value: T) { - -}