import { FilterQuery } from "mongodb";
abstract class Base<T extends Base<any>> {
public static async findOne<T extends Base<any>>(
this: new (...a: any[]) => T,
query: FilterQuery<T>
): Promise<T> {
console.log(this.getSomeString()); // currently: "getSomeString" dosnt exists on type "new (...a: any[]) => T"
// already tried "typeof T" - because "typeof" is not allowed on generics
// already tried https://stackoverflow.com/a/52358194/8944059 's static solution
// already tried "T" - which makes "this: any"
// and i tried various other things, which resulted in "await SomeExtendingClass" cannot be assigned to "this"
// Edit for @jcalz
console.log(this.someProtectedMethod()); // error "someProtectedMethod" does not exists on type "this"
return undefined;
}
public static getSomeString(): string {
this.someProtectedMethod(); // this would work
return "Hello";
}
// Edit for @jcalz
protected static someProtectedMethod() {
return "hello2";
}
}
class SomeExtendingClass extends Base<SomeExtendingClass> {
public someValue: string;
}
(async () => {
await SomeExtendingClass.findOne({ someValue: "hi" });
})();
Goal is to have all statics in this while being able to call it with new this(...args) and while having all keys of (Parent class) insert-able into query and without having to remove abstract
Already tried:
- https://stackoverflow.com/a/58037767/8944059
- https://stackoverflow.com/a/52358194/8944059 's static solution
PS: i dont understand most of the magic of typescript types, that is why i'm asking...
findOne()'sthisparameter type toPick<typeof Base, keyof typeof Base> & (new (...a: any[]) => T)?protectedfunctions / values?typeof Base & (new (...a: any[]) => T)but I'd have to see a full minimal reproducible example to know if it works. The issue withprivateandprotectedmembers is they are intentionally hidden fromkeyof, see microsoft/TypeScript#13543