函数返回类型TS2749

时间:2019-05-21 09:09:19

标签: typescript

如何让Typescript编译器将函数的返回值解释为类类型?

export const produceClass = () => {
    return class Class1 {
        x: number;
        constructor() {
            this.x = 0;
        }
    }
};

export const ProducedClass = produceClass();

class Test2 {
    y: ProducedClass; // Error:(14, 8) TS2749: 'ProducedClass' refers to a value, but is being used as a type here.
    constructor() {
        this.y = new ProducedClass();
    }
}

如果我用手工声明相同的类型,则效果很好:

export class ProducedClass {
    x: number;
    constructor() {
        this.x = 0;
    }
};

class Test2 {
    y: ProducedClass; // ok
    constructor() {
        this.y = new ProducedClass();
    }
}

0 个答案:

没有答案