TypeScript Vuex 插件 "incompatible types"
TypeScript Vuex Plugin "incompatible types"
使用 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 错误:
Types of property 'plugins' are incompatible. Type 'void[]' is not
assignable to type 'Plugin[] | undefined'. Type 'void[]' is not
assignable to type 'Plugin[]'. Type 'void' is not assignable to type
'Plugin'.
我意识到这是一个 TypeScript 问题,并且是 TypeScript 的新手。如果知道解决这个问题的最佳方法,那就太好了。
好吧,你的 ParamsPlugin
是一个不 return 任何东西的函数 (() => void
),所以,当你 ParamsPlugin()
你基本上有 void
. TypeScript 期望 plugins
数组具有 Plugin
类型的对象,而不是 void
.
虽然我不知道 Plugin
是什么,但如果您删除括号,它可能会起作用:
export const ParamsPlugin =
() => (store: Store<RootState>) => {
console.log("ParamsPlugin");
}
使用箭头函数时,如果使用括号,则必须使用 return
到 return 的东西。 return 仅当在 =>
之后放置一个表达式而不是代码块时才隐含。
现在,ParamsPlugin()
returns (Store<RootState>) => void
而不是 void
.
希望这对您有所帮助
使用 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 错误:
Types of property 'plugins' are incompatible. Type 'void[]' is not assignable to type 'Plugin[] | undefined'. Type 'void[]' is not assignable to type 'Plugin[]'. Type 'void' is not assignable to type 'Plugin'.
我意识到这是一个 TypeScript 问题,并且是 TypeScript 的新手。如果知道解决这个问题的最佳方法,那就太好了。
好吧,你的 ParamsPlugin
是一个不 return 任何东西的函数 (() => void
),所以,当你 ParamsPlugin()
你基本上有 void
. TypeScript 期望 plugins
数组具有 Plugin
类型的对象,而不是 void
.
虽然我不知道 Plugin
是什么,但如果您删除括号,它可能会起作用:
export const ParamsPlugin =
() => (store: Store<RootState>) => {
console.log("ParamsPlugin");
}
使用箭头函数时,如果使用括号,则必须使用 return
到 return 的东西。 return 仅当在 =>
之后放置一个表达式而不是代码块时才隐含。
现在,ParamsPlugin()
returns (Store<RootState>) => void
而不是 void
.
希望这对您有所帮助