microsoft / microsoft/TypeScript

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

オープン
#63,189 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: Decorators
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供されている TypeScript Playground の例から始め、experimentalDecorators を有効にした場合と無効にした場合の出力を比較します。デコレートされたコンストラクターパラメーターを持つクラスに対して、static な this 参照を出力するコンパイラーパスを調査します。experimental-decorator の出力によって staticThisRef が MyClass を指し、パラメーターのデコレーションのサポートが維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。