The code below is valid Javascript. Typescript, reasonably, doesn't understand B's this.a
references and calls them out. Is there some type-fu that can be applied to B to explain the run time circumstances to Typescript and get the benefit of type-safety (e.g. an error if trying to access this.c
)?
const A = { a: 1, bar() { this.a = 10; }, }; const B = { b: 2, f() { console.log(this.a + this.b); }, bar() { super.bar(); console.log(this.a); }, }; Reflect.setPrototypeOf(B, A); B.f(); // 3 B.bar(); // 10
没有评论:
发表评论