class SuperClass { ... }
class SubClass1 extends SuperClass { ... }
class SubClass2 extends SuperClass { ... }
class SubClass3 extends SuperClass { ... }
const foo: ??? = ...
Is there a way to assign a type to foo
indicating that it is an instance of any class that extends SuperClass
, without explicitly listing every subclass using union types?
Update:
I am encountering the following issue:
type SomeType = { foo: SuperClass };
public someMethod<T extends SuperClass>() {
const bar: SomeType = ... ;
const someSet: Set<T> = new Set();
someSet.add(bar.foo);
Argument of type 'SuperClass' is not assignable to parameter of type 'T'. 'SuperClass' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'SuperClass'.
Perhaps changing Set<T>
to Set<SuperClass>
would resolve this issue.