So there's this piece of code at the end of Typescript's generics guide
class BeeKeeper {
hasMask: boolean = true;
}
class ZooKeeper {
nametag: string = 'abc';
}
class Animal {
numLegs: number = 123;
}
class Bee extends Animal {
keeper: BeeKeeper = new BeeKeeper();
}
class Lion extends Animal {
keeper: ZooKeeper = new ZooKeeper();
}
function createInstance<A extends Animal>(c: new () => A): A {
return new c();
}
createInstance(Lion).keeper.nametag; // typechecks!
createInstance(Bee).keeper.hasMask; // typechecks!
This program just creates some classes and then defines a method that creates an instance of a given class. A problem arises when we try to do comparisons with variable c though. For example, if we want to check whether c == Lion then Typescript throws an error(playground link). Anyone know a way to do such comparisons while still keeping type checking in place?