class Hello {
private input = { foo };
accessor util = this.input.foo();
}
var hello = new Hello()
console.log(hello);
is transformed to
class Hello {
constructor() {
this.input = { foo };
}
#_util_accessor_storage = this.input.foo();
get util() {
return this.#_util_accessor_storage;
}
set util(value) {
this.#_util_accessor_storage = value;
}
}
var hello = new Hello();
console.log(hello);
(playground).
But this executes this.input.foo() before this.input = and will cause an error.
esbuild avoids this by moving the private class field definition inside the constructor (esbuild try). TypeScript has a similar output:
class Hello {
constructor() {
this.input = { foo };
this.#util_accessor_storage = this.input.foo();
}
#util_accessor_storage;
get util() { return this.#util_accessor_storage; }
set util(value) { this.#util_accessor_storage = value; }
}
var hello = new Hello();
console.log(hello);
Originally reported at vitejs/vite#22212
is transformed to
(playground).
But this executes
this.input.foo()beforethis.input =and will cause an error.esbuild avoids this by moving the private class field definition inside the constructor (esbuild try). TypeScript has a similar output:
Originally reported at vitejs/vite#22212