Using Typescript 2.3.4.
I'm trying to put a generic constraint on a method parameter, like so: public static FromFactType<T extends FactCore>() :T. FactCore is a base class which has three direct descendents: FactEvent, FactDuration, FactPlain. Each of those direct descendents has 15-20 separate children which add new properties. As an example, FactEvent has a child FactBirth, so FactBirth inherits indirectly from FactCore.
However, when I try to pass an instance of FactBirth into my method, I get an error: Type BirthFact cannot be converted to type 'T'. The only way I've been able to appease the Typescript compiler is to explicitly list BirthFact in the constraint. I've tried adding the mid-level classes (FactEvent, FactDuration and FactPlain) to the constaint with no luck - same error.
Do I really need to explicitly list all 60+ bottom-level classes in the constraint?
Thanks.
来源:https://stackoverflow.com/questions/45493893/typescript-generic-constraint-for-child-classes