Typescript: how to return subclass type from inherited static property?

醉酒当歌 提交于 2020-01-21 08:05:32

问题


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?


回答1:


You can create a generic version of find that will have the this parameter inferred to the type of the class it's being called on, and use InstanceType<T> to extract the actual instance type from the class type:

class BaseDoc {
    static Collection: any;
    static find<T extends typeof BaseDoc>(this: T): InstanceType<T> {
        // which is expected to return an instance of SubDoc when run SubDoc.find()
        return this.Collection.find();
    }
}

class SubDoc extends BaseDoc {

}

SubDoc.find() // return SubDoc

Or you can mandate that the derived class defines a generic Collection member of the appropriate type:

class Collection<T> {
    find(): T {
        return null as any // dummy imeplmentation
    }
}
type GetCollectionItem<T extends Collection<any>> = T extends Collection<infer U> ? U: never;

class BaseDoc {
    static Collection: Collection<BaseDoc>;
    static find<T extends { new (... args: any[]) : any, Collection: Collection<any> }>(this: T):  GetCollectionItem<T['Collection']> {
        // which is expected to return an instance of SubDoc when run SubDoc.find()
        return this.Collection.find();
    }
}

class SubDoc extends BaseDoc {
    static Collection: Collection<SubDoc>;
}

SubDoc.find() // return SubDoc


来源:https://stackoverflow.com/questions/51859682/typescript-how-to-return-subclass-type-from-inherited-static-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!