如何在 vue 3 和 typescript 中正确输入 vuex 模块

How to correctly type vuex modules in vue 3 and typescript

我想弄清楚如何在 vue 3 typescript 项目中键入 vuex 模块。这方面缺少官方文档。

假设我有这样一个项目:

import { createStore, useStore as baseUseStore, Store } from 'vuex';
import { InjectionKey } from 'vue';

interface FruitState  {
    apple: boolean,
    peach: boolean,
    plum: boolean
}

const FruitModule = {
    namespaced: true,
    state: (): FruitState => ({
      apple: true,
      peach: false,
      plum: true
    }),
    mutations: {},
    action: {}
}


export interface State {
    foo: string;
  }
  
  export const key: InjectionKey<Store<State>> = Symbol();
  
  export const store = createStore<State>({
      modules: {
        fruit: fruitModule
      },
      state: {foo: 'foo'},
      mutations: { 
        changeFoo(state: State, payload: string){
            state.foo = payload
        }
      },
      actions: { 
        setFooToBar({commit}){
         commit('changeFoo', 'bar')
      }}
  })

  export function useStoreTyped() {
    return baseUseStore(key);
  }
  

...然后在组件中:

  const apple = computed(() => store.state.fruit.apple);

当我尝试访问 apple 时它不起作用,因为它会抛出错误 Property 'fruit' does not exist on type 'State'

现在如果我这样做:

import { createStore, useStore as baseUseStore, Store } from 'vuex';
import { InjectionKey } from 'vue';

interface FruitState  {
    apple: boolean,
    peach: boolean,
    plum: boolean
}

const FruitModule = {
    namespaced: true,
    state: (): FruitState => ({
      apple: true,
      peach: false,
      plum: true,
    }),
    mutations: {},
    action: {}
}


export interface State {
    foo: string;
    fruit?: FruitState;
  }
  
  export const key: InjectionKey<Store<State>> = Symbol();
  
  export const store = createStore<State>({
      modules: {
        fruit: fruitModule
      },
      state: {foo: 'foo'},
      mutations: { 
        changeFoo(state: State, payload: string){
            state.foo = payload
        }
      },
      actions: { 
        setFooToBar({commit}){
         commit('changeFoo', 'bar')
      }}
  })

  export function useStoreTyped() {
    return baseUseStore(key);
  }

再试一次,错误变为Object is possibly 'undefined'

如果我使用可选链接,它将允许我访问水果模块 ?.

const apple = computed(() => store.state.fruit?.apple);

但这对我来说似乎不能接受,因为我知道 fruit.apple 实际上不是未定义的。

在 vuex 的状态类型中包含模块的正确方法是什么?

您不需要在 State 界面中将 fruit 状态设为可选:

export interface State {
  foo: string;
  //fruit?: FruitState; 
  fruit: FruitState;
}

根据您的评论,您在声明根状态时试图解决此 TypeScript 错误(如 here 所示):

export const store = createStore<State>({
  modules: {
    fruit: fruitModule
  },
  state: { foo: 'foo' }, // ❌ Property 'fruit' is missing in type '{ foo: string; }' but required in type 'State'.
})

使用类型断言作为解决方法:

export const store = createStore<State>({
  modules: {
    fruit: fruitModule
  },
  state: { foo: 'foo' } as State, // ✅
})

demo