Reputation: 1550
I have the following TypeScript code:
class ClassA {
field1: any;
}
const SomeClassAsA = diContainer.get('SomeClass') as ClassA;
class ClassB extends ClassA {} // works
class ClassB extends SomeClassAsA {} // error: Type ClassA is not a constructor function type
Why can ClassB be extended with ClassA but not with some other Object casted as ClassA? In need to extend SomeClass. How can I accomplish that?
I use TypeScript 1.5.3
Upvotes: 0
Views: 1518
Reputation: 187034
const SomeClassAsA = diContainer.get('SomeClass') as typeof ClassA;
// ^ added
When you use a class as a type, you are actually typing an instance of that class. Remember, that a class is a real object in javascript, not like an interface
which has no raw javascript corollary.
This makes sense, because it allows you to write:
const person: Person = new Person()
person
is clearly and an instance of Person
and not the class constructor object Person
.
So to get the the type of the class itself, use the typeof
operator.
Upvotes: 1