typescript - ActionAugmentation 的通用类型
问题描述
我正在为 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
解决方案
推荐阅读
- python - 可以在没有 GUI 的情况下完全使用 DaVinci Resolve python API 吗?
- ruby-on-rails - 切换到 structure.sql 后删除/保留 schema.rb
- swift - Swift 中未对齐的 UICollectionViewCells
- javascript - 使用ajax将照片和多个数据发送到控制器
- python - 在python中按文件名的一部分查找文件
- optimization - 如何最小化 Google OR-Tools 中的最大值?
- templates - 如何向现有问题数据集中的问题添加不同的短语?
- javascript - 如何使用 JavaScript querySelector() 编辑表格行?
- flutter - 在 webview_flutter 中导航时如何防止深度链接/单链接
- javascript - 需要帮助添加 playVideo();