提问人:Samantha Márquez 提问时间:4/17/2023 最后编辑:Samantha Márquez 更新时间:4/17/2023 访问量:64
“this”隐式具有类型“any”,因为它没有类型注释 (TypeScript)
'this' implicitly has type 'any' because it does not have a type annotation (TypeScript)
问:
我有一个文件.ts,并收到以下代码的此错误:
const hulk={
nombre:'Hulk',
smash(){
setTimeout(function(){
console.log(`${ this.nombre} smash!`); //HERE is the error but IDK why
},1000);
setTimeout(()=>{
console.log(`${ this.nombre} smash!`);
},1000);
}
}
hulk.smash();
我正在学习使用箭头函数以及使用箭头函数和使用函数之间的区别,但是当我在函数中输入“this”一词时,我遇到了错误,因为当我在箭头函数中使用“this”时,一切都很好。
我试着添加
"noImplicitAny": false,
自
tsconfig.json
但这没有用。
答:
0赞
René
4/17/2023
#1
this
inside 指的是函数上下文,而不是 Hulk 对象。箭头函数不会创建自己的上下文(它们自己的 this),因为它们继承了它。setTimeout
评论