【发布时间】:2020-06-16 00:54:56
【问题描述】:
我在我的 Vuex 组件中使用 Typescript,并希望为 mapState 中的映射函数提供类型。直觉上,我是这样写的:
@Component({
computed: {
...mapState( MY_NAMESPACE, {
fooIndex: ( state: MyModel ) => state.values.indexOf('foo')
})
// more computed props here
}
})
export default class MyComponent extends Vue {}
这在过去有效,但是在升级我的依赖项后它不再有效,我收到错误 No overload matches this call.
Overload 1 of 6, '(namespace: string, map: string[]): { [x: string]: Computed; }', gave the following error.
作为一种解决方法,我可以从函数参数中删除类型并像这样进行转换:
@Component({
computed: {
...mapState( MY_NAMESPACE, {
fooIndex: state => (state as MyModel).values.indexOf('foo')
}),
}
})
export default class MyComponent extends Vue {}
有没有更好的表达类型的方法?
【问题讨论】:
-
您正在使用Class style component。
vuex-class有帮助吗? -
您解决了吗?我遇到了同样的问题。
标签: typescript vue.js vuex