class BaseDoc {
static Collection;
static find(){
// which is expected to return an instance of SubDoc when run SubDoc.find()
return this.Collection.find();
}
}
class SubDoc extends BaseDoc {}
What I hope is: When running SubDoc.find(), the app knows the type of the return value to be an instance of SubDoc rather than BaseDoc.
How can I achieve that?
You can create a generic version of
find
that will have thethis
parameter inferred to the type of the class it's being called on, and useInstanceType<T>
to extract the actual instance type from the class type:Or you can mandate that the derived class defines a generic
Collection
member of the appropriate type: