I want to write es6 class:
class SomeClass {
static prop = 123
method() {
}
}
How to get access to static prop
from method()
without use SomeClass
explicitly? In es6 it can be done with this.constructor
, but in typescript this.constructor.prop
causes error "TS2339: Property 'prop' does not exist on type 'Function'".
but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'".
Typescript does not infer the type of constructor
to be anything beyond Function
(after all ... the constructor might be a sub class).
So use an assertion:
class SomeClass {
static prop = 123;
method() {
(this.constructor as typeof SomeClass).prop;
}
}