将静态成员绕过到装饰类

时间:2018-01-30 14:45:42

标签: typescript decorator

class SmthImpl {
    static readonly staticConst = 7;
}

function decorator(opts?: any) {
    return function (Class: { new() }) {
        return class Wrapper {
            // ...
        };
    }
}

const Smth = decorator()(SmthImpl);

现在我想访问Smth.staticConst,但它不在那里:playground

  

Property' staticConst'类型' typeof Wrapper'。

上不存在

我试图添加

Object.assign(Smth, SmthImpl);

但是typescript会发出同样的错误。

如何编写代码以访问静态成员?

1 个答案:

答案 0 :(得分:1)

您需要将{ "jest.pathToJest": "node_modules/jest/bin/jest.js" } 参数更改为扩展构造函数的泛型类型Class。不同之处在于T将被推断为整个类(T),而不仅仅是typeof SmthImpl的构造函数。此外,您需要使用SmthImpl作为Class的基本类型,目前您不将它用于函数体中的任何内容:

Wrapper
相关问题