[英]typescript compilation error - “Value of type … is not indexable by type …”
Using typescript 2.0.6.0 使用打字稿2.0.6.0
I have an interface defined: 我定义了一个接口:
interface indexesWorkData {
pointInTime: number;
numberOfIndexesWorking: number;
}
This is my class: 这是我的课:
class work {
indexesWork: Array<indexesWorkData> = [];
pushRange(startTime: number, endTime: number) {
let indexStart = this.indexesWork.findIndex(x => x.pointInTime >= someNumber);
if (this.indexesWork[indexStart].pointInTime === anotherNumber) {
// Do stuff
}
}
}
I get compilation error: "Value of type indexesWorkData[] is not indexable by type indexesWorkData" for trying to access this.indexesWork[indexStart
] 我收到编译错误:尝试访问
this.indexesWork[indexStart
]时出现“ indexsWorkData []类型的值不能被indexsWorkData类型索引”
But at runtime it works and indexStart has correct value. 但是在运行时它可以工作,并且indexStart具有正确的值。
Why is that ? 这是为什么 ?
您能否尝试明确告诉编译器有关indexStart
的类型?
let indexStart: number = <number>this.indexesWork.findIndex(x => x.pointInTime >= someNumber);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.