【发布时间】:2019-05-23 22:22:40
【问题描述】:
Typescript 在以下代码中引发错误 "Element implicitly has an 'any' type because type 'HumansToDogs' has no index signature."。
对我来说,一切似乎都是明确而直接的,有人可以帮忙吗?
type HumanName = 'Jessie' | 'Mark';
type DogName = 'Spot' | 'Buddy';
type HumansToDogs = {
[key in HumanName]: DogName; // Isn't this an index signature?
}
const humansToDogs: HumansToDogs = {
'Jessie': 'Buddy',
'Mark': 'Spot',
};
for (const human in humansToDogs) {
const dog = humansToDogs[human]; // Index signature error here
}
【问题讨论】:
标签: javascript typescript types typescript-typings