microsoft / microsoft/TypeScript

Incorrect reference `this` in static context with `experimentalDecorators` enabled

Ouverte
#63,189 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: Decorators
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

### 🔎 Search Terms

"static this", "experimental decorators"

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about static

### ⏯ Playground Link

https://www.typescriptlang.org/play/?experimentalDecorators=true&target=7&ts=5.9.3&emitDecoratorMetadata=true#code/GYVwdgxgLglg9mABAEQKYTgJwIZS4gCim0wHNUoAuRAMXGnjABpEBrVAT2oGcpMYwpRAB9E4ACapgA1OJYDJAD2pgQAWwBGqTAEpEAbwC+AKGMQANtm7dEAWQ4BhADJWb+44kS9cMCIkyo2OII5hxexLAQACoAFjDcAEpSiAC8iFBx3ADcHogYYLyYINBYBAACaBg4eJiIwHBwKupaugYmJkA

### 💻 Code

```ts
function Decorator (target: Function, key: string | undefined, index: number) {}

class MyClass {
static readonly staticThisRef = this;
constructor(@Decorator foo: number) {}
}
```

### 🙁 Actual behavior

`MyClass.staticThisRef = (void 0);`

### 🙂 Expected behavior

`staticThisRef` points to the class.

### Additional information about the issue

Enabling or disabling `experimentalDecorators` fixes the issue.

With `experimentalDecorators`:
```ts
let MyClass = class MyClass {
constructor(foo) { }
};
MyClass.staticThisRef = (void 0);
MyClass = __decorate([
__param(0, Decorator)
], MyClass);
```

Without `experimentalDecorators`:
```
class MyClass {
constructor(foo) { }
}
_a = MyClass;
MyCLass.staticThisRef = _a;
```

However only experimental decorators support decorating parameters.

Also TypeScript compiler behavior here is different from esbuild with `experimentalDecorators` feature enabled:

https://esbuild.github.io/try/#dAAwLjI3LjMALS1sb2FkZXI9dHMgIi0tdHNjb25maWctcmF3PXtcImNvbXBpbGVyT3B0aW9uc1wiOntcImV4cGVyaW1lbnRhbERlY29yYXRvcnNcIjp0cnVlfX0iAGZ1bmN0aW9uIERlY29yYXRvciAodGFyZ2V0OiBGdW5jdGlvbiwga2V5OiBzdHJpbmcgfCB1bmRlZmluZWQsIGluZGV4OiBudW1iZXIpIHt9CgpjbGFzcyBNeUNMYXNzIHsKICBzdGF0aWMgcmVhZG9ubHkgc3RhdGljVGhpc1JlZiA9IHRoaXM7CiAgY29uc3RydWN0b3IoQERlY29yYXRvciBmb286IG51bWJlcikge30KfQ

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par l’exemple TypeScript Playground fourni et comparez la sortie émise avec et sans experimentalDecorators. Étudiez le chemin du compilateur qui émet la référence statique à this pour une classe avec un paramètre de constructeur décoré. Le travail est terminé lorsque la sortie experimental-decorator fait pointer staticThisRef vers MyClass tout en préservant la prise en charge de la décoration des paramètres.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
38/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.