gpt4 book ai didi

Vuex 模块中的 TypeScript 错误 - 子模块无法分配到 Vuex 的索引模块中

转载 作者:行者123 更新时间:2023-12-05 05:09:12 31 4
gpt4 key购买 nike

我正在使用 TypeScript 构建一个简单的 vue 项目。我在我的 vue 项目中编写了 vuex 商店,该商店有一个名为“计算”的子模块。但是一旦创建了包含子模块的主存储,就会发生奇怪的错误(tserror)。

以下是我的 vuex 代码。

// calculation.ts
import { Module } from 'vuex'

interface ICalculationState {
a: number
}

const calculation: Module<ICalculationState, {}> = {
namespaced: true,

state: {
a: 10
}
}

export {
calculation,
ICalculationState
}
// index.ts
import Vue from 'vue'
import Vuex from 'vuex'

import { calculation, ICalculationState } from './calculation'

Vue.use(Vuex)

interface IState {
calculation: ICalculationState
}

const store = new Vuex.Store<IState>({
modules: {
calculation
}
})

export { store, IState }

错误字符串

Type '{ calculation: Module<ICalculationState, {}>; }' is not assignable to type 'ModuleTree<IState>'.
Property 'calculation' is incompatible with index signature.
Type 'Module<ICalculationState, {}>' is not assignable to type 'Module<any, IState>'.
Types of property 'actions' are incompatible.
Type 'ActionTree<ICalculationState, {}> | undefined' is not assignable to type 'ActionTree<any, IState> | undefined'.
Type 'ActionTree<ICalculationState, {}>' is not assignable to type 'ActionTree<any, IState>'.
Index signatures are incompatible.
Type 'Action<ICalculationState, {}>' is not assignable to type 'Action<any, IState>'.
Type 'ActionHandler<ICalculationState, {}>' is not assignable to type 'Action<any, IState>'.
Type 'ActionHandler<ICalculationState, {}>' is not assignable to type 'ActionHandler<any, IState>'.
The 'this' types of each signature are incompatible.
Type 'Store<IState>' is not assignable to type 'Store<{}>'.
Types of property 'registerModule' are incompatible.
Type '{ <T>(path: string, module: Module<T, IState>, options?: ModuleOptions | undefined): void; <T>(path: string[], module: Module<T, IState>, options?: ModuleOptions | undefined): void; }' is not assignable to type '{ <T>(path: string, module: Module<T, {}>, options?: ModuleOptions | undefined): void; <T>(path: string[], module: Module<T, {}>, options?: ModuleOptions | undefined): void; }'.
Types of parameters 'module' and 'module' are incompatible.
Type 'Module<any, {}>' is not assignable to type 'Module<any, IState>'.
Types of property 'actions' are incompatible.
Type 'ActionTree<any, {}> | undefined' is not assignable to type 'ActionTree<any, IState> | undefined'.
Type 'ActionTree<any, {}>' is not assignable to type 'ActionTree<any, IState>'.
Index signatures are incompatible.
Type 'Action<any, {}>' is not assignable to type 'Action<any, IState>'.
Type 'ActionHandler<any, {}>' is not assignable to type 'Action<any, IState>'.
Type 'ActionHandler<any, {}>' is not assignable to type 'ActionHandler<any, IState>'.
Type '{}' is not assignable to type 'IState'.ts(2322)
index.ts(9, 3): 'calculation' is declared here.
index.d.ts(95, 3): The expected type comes from property 'modules' which is declared here on type 'StoreOptions<IState>'

我希望红色下划线从我的代码所在的行下方消失。

最佳答案

在 calculation.ts 中提供 IState 作为“模块”中的第二个参数。通过这种方式,您定义了根状态,错误消失了。

const calculation: Module<ICalculationState, IState>

关于Vuex 模块中的 TypeScript 错误 - 子模块无法分配到 Vuex 的索引模块中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731741/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com