首页 文章

TypeScript Vuex插件“不兼容的类型”

提问于
浏览
0

使用TypeScript为vuex商店创建vuex插件时遇到问题 . 该插件使用更高阶函数来获取参数,如下所示:

Plugin.ts

export const ParamsPlugin = 
       () => {
            (store: Store<RootState>) => {
                console.log("ParamsPlugin");
            } 
        }

Index.ts

const store :StoreOptions<RootState> = {
    state: <RootState> {
      ...
    },
    mutations: {
      ...
    },
    modules: {
      ...
    },
     plugins:  [ ParamsPlugin() ]
  }

Typescript错误:

属性“插件”的类型不兼容 . 类型'void []'不能分配给'Plugin [] |未定义” . 类型'void []'不能分配给'Plugin []'类型 . 类型'void'不能分配给'Plugin'类型 .

我意识到这是一个TypeScript问题,并且是TypeScript的新手 . 知道解决这个问题的最佳方法会很棒 .

1 回答

  • 0

    好吧,你的 ParamsPlugin 是一个不返回任何东西的函数( () => void ),所以,当你做 ParamsPlugin() 时,你基本上有 void . TypeScript期望 plugins 数组具有 Plugin 类型的对象,而不是 void .

    虽然我不知道 Plugin 是什么,但是如果你删除括号可能会有效:

    export const ParamsPlugin = 
       () => (store: Store<RootState>) => {
                console.log("ParamsPlugin");
             }
    

    使用箭头功能时,如果使用括号,则必须使用 return 返回某些内容 . 只有在 => 之后放置表达式而不是代码块时,返回才是隐式的 .

    现在, ParamsPlugin() 返回 (Store<RootState>) => void 而不是 void .

    希望这可以帮助

相关问题