首页 > 解决方案 > 为什么 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
        }
    }
}

标签: typescriptarrow-functions

解决方案


推荐阅读