I am using the extends
constraint in TypeScript, like so:
class Animal {}
class Lion extends Animal {}
class Bear extends Animal {}
class ZooKeeper<T extends Animal> {
constructor(p: T = new Animal()) {
}
}
new ZooKeeper(new Animal());
But p: T = new Animal()
contains an error:
Type 'Animal' is not assignable to type 'T'.
constructor Animal(): Animal
Why, and what do I do, in order that I can use Animal
, in place of Animal
subtypes?
Source
Cast your animal to T and it will work.
class ZooKeeper<T extends Animal> {
constructor(p: T = <T>new Animal()) {
}
}
According to your own comment (for future readers) you can also do:
class ZooKeeper<T extends Animal> {
constructor(p: T = new Animal() as T) {
}
}
You could just leave the assignment from the constructor parameter declaration, seems to be compiling properly for me. You can even make the parameter optional, if you may want to leave it.
UPDATE: As series0ne pointed out, this solution does not provides a default instance of Animal
where one has not been provided!
class ZooKeeper<T extends Animal> {
constructor(p?: T) {
}
}
var zk = new ZooKeeper<Lion>(new Lion());
var zk2 = new ZooKeeper(new Animal());
var zk3 = new ZooKeeper();
The ZooKeeper
class will be typed and you can access the child class specific properties of T
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With