【发布时间】:2020-07-17 12:52:26
【问题描述】:
使用下面的代码,TypeScript 将无法识别 thisArg 的上下文(应该是 a 并且应该有 saySomething() 方法。还有其他方法可以为 TypeScript 提供 thisArg 的上下文吗?
代码:
class A {
saySomething() {
console.log('Hello!');
}
}
class B {
constructor(a: A) {
const boundSaySomething = this.saySomethingMyself.bind(a);
boundSaySomething();
}
saySomethingMyself() {
this.saySomething();
}
}
const test = new B(new A());
控制台正确记录Hello,但类型检查显示
Property 'saySomething' does not exist on type 'B'.(2339)
【问题讨论】:
标签: typescript this typechecking