【发布时间】:2019-09-30 12:21:33
【问题描述】:
我想通过这个类的一个对象来访问一个类的静态成员。
我使用obj.constructor 进行操作。它工作得很好,除了打字稿 linter 说
“函数”类型上不存在属性“getName”
class Foo {
public static getName(): string {
return 'foo';
}
}
const foo = new Foo();
const name: string = foo.constructor.getName();
我尝试使用:const name: string = (foo.constructor as Foo).getName();
但它给了我
属性“getName”是“Foo”类型的静态成员
编辑:
它使用:const name: string = (foo.constructor as typeof Foo).getName();
有没有什么方法可以在不手动转换类的情况下工作?
INFO : 在我的具体情况下,我不能直接使用 Foo.getName() 调用它
【问题讨论】:
标签: typescript class typescript-typings