Javascript 流:计算属性。在…中找不到可索引签名。。。当类型为class时
如何修复这种情况下的错误(继续使用类作为类型): 错误指向Javascript 流:计算属性。在…中找不到可索引签名。。。当类型为class时,javascript,ecmascript-6,flowtype,Javascript,Ecmascript 6,Flowtype,如何修复这种情况下的错误(继续使用类作为类型): 错误指向person[key],并显示: 无法获取person[key],因为中缺少索引器属性 Person 在类似情况下(可能是不同的流程版本),它表示: 计算属性。在Person 我认为更大的问题是Array.prototype.forEach具有 forEach(callbackfn: (value: T, index: number, array: $ReadOnlyArray<T>) => any, thisArg?:
person[key]
,并显示:
无法获取person[key]
,因为中缺少索引器属性
Person
在类似情况下(可能是不同的流程版本),它表示:
计算属性。在Person
我认为更大的问题是
Array.prototype.forEach
具有
forEach(callbackfn: (value: T, index: number, array: $ReadOnlyArray<T>) => any, thisArg?: any): void;
不一定是因为它有用,而是因为它非常明确,应该是安全的访问
我会尽量避免在属性列表中使用循环来获取/设置对象的属性,因为Flow还没有处理类似元组的数组的细微差别。此外,如果对象的属性发生更改,则在对象本身(例如,object.keys
)中循环更能证明未来的可行性
forEach(callbackfn: (value: T, index: number, array: $ReadOnlyArray<T>) => any, thisArg?: any): void;
/* @flow */
class Person {
name: string;
age: number;
}
const person: Person = new Person();
person.name = 'Bob';
person.age = 25;
['name', 'age'].forEach(key => {
if (key === 'name') {
console.log(person[key]);
}
});