Could anyone help me understand why this code isn't functioning as intended:
class Fooable {
foo: string;
}
class Barable extends Fooable {
bar: boolean;
}
function simplifiedExample<T extends Fooable>(): Array<T> {
let list = new Array<T>();
list.push(new Barable());
return list;
}
Barable is extending Fooable. Why can't I add Barable to an Array when T must be Fooable? Check out the code in this playground
EDIT:
The issue stems from simplifiedExample() being an override from a base class, where the base class is solely a definition due to the project being a mix of JavaScript and TypeScript.
Refer to: new playground
I managed to find a workaround using casting, but it feels like more of a temporary fix than a proper solution:
class BarableService extends FooableService {
simplifiedExample<T extends Fooable>(): Array<T> {
let list = new Array<Fooable>();
list.push(new Barable());
return list as Array<T>;
}
}