microsoft / microsoft/TypeScript

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

Abierto
#63,189 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: Decorators
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

🔎 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
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:

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el ejemplo proporcionado de TypeScript Playground y compara la salida emitida con y sin experimentalDecorators. Investiga la ruta del compilador que emite la referencia estática a this para una clase con un parámetro de constructor decorado. Se considera terminado cuando la salida de experimental-decorator hace que staticThisRef apunte a MyClass, manteniendo al mismo tiempo la compatibilidad con la decoración de parámetros.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.