proposal-decorators
proposal-decorators copied to clipboard
Yet another unexpected initializer ordering causes TDZ errors
This throws:
function dec2(_: any, context: any) {
context.addInitializer(function (this: any) {
console.log(this[context.name]) // Boom! Error! "Cannot read private member from an object whose class did not declare it"
})
}
class Odd {
#foo = 123
@dec2 get foo() {
return this.#foo
}
@dec2 set foo(v) {
this.#foo = v
}
}
new Odd()
TypeScript playground Babel repl
It fundamentally is very unintuitive for that to throw.
I continue to believe that source-ordered initialization would be the most suitable and flexible: if you have ordering issues, just change the order of the members in your class. The author would be in control.
At the moment, ordering is hard coded in the spec rather arbitrarily, and unintuitive:
- https://github.com/tc39/proposal-decorators/issues/566
- https://github.com/tc39/proposal-decorators/issues/565