vuejs2 - TypeScript-fel i Vuex-modul - Något är fel så att undermodulen inte kan tilldela till indexmodulen i Vuex
Jag bygger ett enkelt Vue-projekt med TypeScript. Jag skrev Vuex-butiken i mitt Vue-projekt, och butiken har en undermodul kallad 'calculation'. Men när jag väl skapat huvudbutiken inklusive undermodulen, uppstår ett konstigt fel (tserror).
Följande är min vuex-kod.
// 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 }
Felsträng
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>'
Jag vill att röd understrykning ska försvinna från raden under där min kod är.
Svar #1
I calculation.ts, ge IState som andra parameter i "Module". På detta sätt definierar du ditt rot-tillstånd och felet är borta.
const calculation: Module<ICalculationState, IState>
