In a static method in a base class, is it possible to name the currently class's instance type?
Put another way, can you make this code type check:
class Base {
static addInitializer(i: (v: any /* What type would go here? */) => void) {
// implementation is irrelevent
}
}
class Dog extends Base {
bark() {}
}
class Fish extends Base {
swim() {}
}
Dog.addInitializer((dog) => {
dog.bark();
// @ts-expect-error
dog.swim();
});
Fish.addInitializer((fish) => {
fish.swim();
// @ts-expect-error
fish.bark();
});
Note the use of // @ts-expect-error before lines that should be a type error. They're highlighted as 'unused' because v has type any rather than the type I'm trying to name here.