如何为打字稿类定义返回类型

时间:2019-05-19 14:39:29

标签: typescript

是否可以通过某种方式为班级设置隐式自定义返回类型? 例如:

class A { }
class B {
    constructor() {
        const a = new A();
        return a;
    }
}
const a = new B(); // here I need `a` to be type of A in typescript, but it is B
console.log(a instanceof A); // => true

我尝试过:

class B {
    constructor() {
        const a = new A();
        return a as A;
        // return a as any as A;
    }
}

0 个答案:

没有答案