首页 > 其他 > 详细

TypeScript静态方法继承

时间:2018-09-06 13:57:03      阅读:306      评论:0      收藏:0      [点我收藏+]

同步版本

class Foo {
    static boo<T extends typeof Foo>(this: T, a: number): InstanceType<T> {
        return (new this()) as InstanceType<T>;
    }
}

class Bar extends Foo {}

// b: Bar
let b = Bar.boo(1);

异步版本

class Foo {
    static async boo<T extends typeof Foo>(this: T, a: number): Promise<InstanceType<T>> {
        const instance = new this();
        await instance.create();
        return <InstanceType<T>>instance;
    }
    private create(){}
}

class Bar extends Foo {}

(async () => {
    // b: Bar
    let b = await Bar.boo(1);
})();

引用自:https://github.com/Microsoft/TypeScript/issues/5863

TypeScript静态方法继承

原文:https://www.cnblogs.com/ystrdy/p/9597508.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!