【发布时间】:2016-01-28 00:08:50
【问题描述】:
我要写es6类:
class SomeClass {
static prop = 123
method() {
}
}
如何在不显式使用SomeClass 的情况下从method() 访问静态prop?在 es6 中可以使用 this.constructor 完成,但在 typescript 中 this.constructor.prop 会导致错误“TS2339: 类型 'Function' 上不存在属性 'prop'”。
【问题讨论】:
-
你试过'this.constructor["prop"]'吗?
-
这不是一个解决方案:在这种情况下,我完全错过了类型 chek。我想在访问不存在的属性时出错。
-
你能分享你的实际代码吗?您在什么情况下尝试这样做?
-
@ArturEshenbrener 你可以做
(this.constructor as typeof SomeClass).prop,但有什么意义呢?为什么不SomeClass.prop?
标签: typescript this static-methods static-members typescript1.6