microsoft / microsoft/TypeScript
Experimental decorators do not get access to the initial value when using auto-accessors
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
In order to give a good out-of-the-box experience with our library that vends decorators, we are trying to make a single set of decorators that works under the following configurations:
- Experimental decorators on class fields (with
useDefineForClassFields: false) - Standard decorators on auto-accessors
- Experimental decorators on auto-accessors
Case (3) may seem unusual at first, but it's very important for incremental migration from experimental decorators to standard decorators. The goal is to enable experimental decorators to be callsite compatible with standard decorators so that callsites and files can be upgraded one-at-a-time without changing the tsconfig, and after the callsites are updated experimentalDecorators can be turned off.
This mostly works except that in case (3) the decorators cannot get access to the initial field value because it's set directly to the auto-accessor backing store and skips the setter. In case (1) the initial value goes through the setter, in case (2) it goes through the init() callback, but in case (3) we can't see it at all.
Case (3) is actually important for multiple reasons:
- Incremental migration as mentioned
- It enables one version of decorator docs (aside from talking about configs) and decorator-using code samples through our docs and example repositories.
- It enables projects to start with or migrate to standard decorators and change back to experimental decorators in case the size penalty of standard decorators is too much (we're seeing a ~50% code size penalty on porting a library to standard decorators above the size with experimental decorators) by just changing the tsconfig.
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about decorators
⏯ Playground Link
💻 Code
const test = (
protoOrDescriptor: Object,
name: PropertyKey,
descriptor: PropertyDescriptor
): any => {
return {
get() {
console.log('get');
return descriptor.get!.call(this);
},
set(v: unknown) {
console.log('set', v);
descriptor.set!.call(this, v);
}
}
}
class A {
@test accessor foo = 'bar';
}
// Expected console: "set", "bar"
const a = new A();
// logs: "set", "baz"
a.foo = 'baz';
🙁 Actual behavior
The setter isn't called, since the output initializes the private storage directly:
class A {
#foo_accessor_storage = 'bar';
get foo() { return this.#foo_accessor_storage; }
set foo(value) { this.#foo_accessor_storage = value; }
}
🙂 Expected behavior
The setter is called. Initializers for decorated accessors under experimental decorators are called as statements in the constructor:
class A {
#foo_accessor_storage;
get foo() { return this.#foo_accessor_storage; }
set foo(value) { this.#foo_accessor_storage = value; }
constructor() {
this.foo = 'bar';
}
}
Additional information about the issue
No response
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das Verhalten im verlinkten TypeScript Playground mit experimental decorators und auto-accessors zu reproduzieren, und vergleiche anschließend das im Issue gezeigte ausgegebene JavaScript. Verfolge den Compilerpfad, der die Initialisierung von auto-accessors ausgibt, und füge eine Regressionstestabdeckung für die erwartete Zuweisung im Konstruktor hinzu. Erledigt ist die Aufgabe, wenn der Anfangswert den dekorierten setter erreicht und nachfolgende Zuweisungen weiterhin funktionieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100