简体   繁体   English

错误TS2339:类型“ {}”上不存在属性“ forEach”

[英]error TS2339: Property 'forEach' does not exist on type '{}'

When I'm trying to compile this typescript with npm it told me this error: "error TS2339: Property 'forEach' does not exist on type '{}'." 当我尝试使用npm编译此打字稿时,它告诉我这个错误: "error TS2339: Property 'forEach' does not exist on type '{}'."

the code is 该代码是

export class TestComponent{
    array: yo[];
    check(){
        this.array.map(abc=>{
            abc.prop.forEach(ws=>{
                console.log(ws)
            })
        });
    }
}

but when I force the execute of the code it works perfectly :\\ 但是当我强制执行代码时,它可以完美运行:\\

The code works because TypeScript will emit the JavaScript even if there's an error during compilation. 该代码有效,因为即使编译期间发生错误,TypeScript也会发出JavaScript。 And because of the nature of JavaScript abc.prop probably is an array and has the forEach method. 并且由于JavaScript的特性,abc.prop可能是一个数组,并具有forEach方法。

And the error occurs because TypeScript can't know that abc.prop is an array. 由于TypeScript无法知道abc.prop是一个数组,因此会发生错误。 You have to either import the appropriate typings or create one for it. 您必须导入适当的类型或为此创建一个类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM