typescript - 为什么 Typescript 在 foreach 中使用匿名函数时不显示错误,但在使用箭头函数时抛出正确的错误?
问题描述
在打字稿中,我有以下引发错误的代码,我知道“each”无效,我必须使用“forEach”
objTablesToCheck.forEach(objTable => {
for (let i = aggregateStartIdx; i < numOfCells; i++) {
//TypeScript thorws error at compile time and I know "each" is not valid and I have to use forEach
//getNthColumnCells return type is Array<HTMLTableCellElement>
this.getNthColumnCells(objTable, colNum).each(objCell => {
//my Logic
}
}
}
但是,TypeScript 不会在编译时抛出错误?为什么“每个”对这个代码块有效?
objTablesToCheck.forEach(objTable => {
for (let i = aggregateStartIdx; i < numOfCells; i++) {
//TypeScript does NOT throw error at compile time
//getNthColumnCells return type is Array<HTMLTableCellElement>
this.getNthColumnCells(objTable, colNum).each(function(objCell) {
//my Logic
}
}
}
解决方案
推荐阅读
- sorting - 按字符串的最后一个字符对压缩列表(字符串,'Line2D' 的实例)进行排序
- typescript - 为什么需要虚拟属性才能使类型保护起作用?
- python - 基于不同值掩码的数组求和
- r - 将国家分为热带与非热带 R
- swift - 循环一系列 withAnimation 函数
- c++ - 在延迟加载的共享库中使用单例
- python - IndexError:列表索引超出范围,即使打印单个列表和索引似乎没有错误
- docker - docker容器中的rsyslog服务无法执行logrotation
- reactjs - 使用此处地图时出现授权错误
- excel - 如何识别分配给形状的宏