'"Transitive type conversion" in typescript
in a typescript project which I'm currently working on, I have a class A, a class B extends A, a class C extends B and a class D extends C. I also have a method getFoo(): A.
The following type conversion doesn't work:
const foo = this.getFoo() as D;
The error I get is that the conversion may be a mistake, because neither type sufficiently overlaps the other (although D is a subclass of A).
What, funnily enough, works, is:
const foo = this.getFoo() as C as D
Why? Is the inheritance chain somehow too long for TS? Is there any way to write this without the double cast?
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
