1

我正在为 Vue3 尝试新的 Vuex4,并且想知道如何实现通用的 ActionAugment,以便 Mutation 和 MutationTypes 是通用的,并且每个模块都可以使用这种通用类型。

import { ActionContext } from 'vuex';

// globalStore.ts

type State = {
  app: AppState;
}

// How can I make use of M and MT, to avoid AppMutation and AppMutationType
type ActionAugment<S, M, MT> = Omit<ActionContext<S, State>, 'commit'> & {
  commit(
    key: AppMutationType,
    payload: Parameters<AppMutation[AppMutationType]>[1]
  ): ReturnType<AppMutation[AppMutationType]>;
}

// end globalStore.ts

// appStore.ts

type AppState = {
  isLoaded: boolean;
};

enum AppMutationType {
  Mutation = 'MUTATION',
}

enum AppActionType {
  Action = 'ACTION',
}

type AppMutation = {
  [AppMutationType.Mutation](state: AppState, isLoaded: boolean): void;
}

const actions = {
  [AppActionType.Action]({ commit }: ActionAugment<AppState, AppMutation, AppMutationType>, isLoaded: boolean) {
    commit(AppMutationType.Mutation, isLoaded);
  },
};

// end appStore.ts
4

0 回答 0