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

时间:2019-08-30 18:37:15

标签: typescript vuejs2 vuex vuex-modules

我正在使用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>'

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

1 个答案:

答案 0 :(得分:1)

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

const calculation: Module<ICalculationState, IState>