带有打字稿的 Vuex 4 - 使用“this”分配初始状态属性值会导致打字稿错误

时间:2021-01-29 06:04:24

标签: typescript vuex

我有一个像这样的简单 vuex 状态。我的目标是最初将 currentScreen 属性设置为 screenList

的第一个元素
...

state: {
 screenList:[{...},{...}], // typed as Array<Screen>
 currentScreen: this.screenList[0] //typed as Screen
}

但是 typescript 编译器遇到以下错误:

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

据我所知,这是因为 this 的上下文可能会根据它的调用位置而变化,因此 screenList 没有明确输入。

如何通过以兼容打字稿的方式引用状态 currentScreen 属性来设置 screenList

0 个答案:

没有答案