如何消除这个 tsc-strict linter 错误?

问题描述 投票:0回答:0

我有以下打字稿代码。

type myType = {
  a: number,
  b: number,
};

const c:myType = {a: 1, b:2};
const d:myType = {a: 3, b:4};
Object.entries(c)
    .forEach(([key, value]) => {
        console.log(d[key])
      }
    )
})

它按预期工作。它打印出

3\n4\n
。 但是,当我使用严格标志在此代码(javascript 编译器的打字稿)上运行
tsc
时,它抱怨这一行:

myFile.ts:777:19 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'myType'.
  No index signature with a parameter of type 'string' was found on type 'myType'.

777       console.log(d[key]);
                  ~~~~~~

为什么我不能使用字符串键来索引 myType?我怎样才能消除这个错误? 同样,代码实际上运行正常。

typescript lint
© www.soinside.com 2019 - 2024. All rights reserved.