我正在尝试创建一个接口(interface),但在实现它时出现错误。 错误:无法在模块或命名空间主体中引用 TS2331“this”。
module myInterface {
interface IPerson {
name: string;
age: number;
greet: () => void;
myAge: () => void;
}
var person: IPerson = {
name: "Gautam",
age: 1,
greet: () => {
console.log("Hey, " + this.name);
},
myAge: () => { console.log(this.age); }
}
var greetMe = person.greet();
var agee = person.myAge();
}
最佳答案
来自您的代码:
{
name: "Gautam",
age: 1,
greet: () => {
console.log("Hey, " + this.name);
},
}
this.name
不引用 name: "Gautam"
因为箭头函数如何与 this
一起工作>。这只是 TypeScript 的另一种情况 preventing bad JavaScript .
更多
关于javascript - TS2331 'this' 无法在模块或命名空间主体中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877695/