2021年4月24日星期六

How to describe prototypal inheritance in Typescript?

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
https://stackoverflow.com/questions/67248701/how-to-describe-prototypal-inheritance-in-typescript April 25, 2021 at 08:32AM

没有评论:

发表评论