microsoft / microsoft/TypeScript

Private class field helpers throw a confusing error when in unbound method with undefined `this`

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

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

this undefined bind private identifier class __classPrivateFieldSet __classPrivateFieldGet bound unbound

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAEBiD29oG8BQ0PQMQDcwgFcBTaAXmgEYAmAZgBYBudTAcyIBcA1fYgCgEoU0AE4cCwgHbR2ACwCWEAHS4eRBtAC+qLamDwJEdtABmiMtAlEA7nEQCmu-RHggiikPBa8ABqfiK2LlUBcwASZD8Ajm5CIgENb34HAHpk6AB5eAAHCAAaaABJaAATOWKJAHIjACM5CWKAQkcDI0CY4nNIttUUtIAVGWF4KwgALmgAYTAJCXgjUTBi6CzhOTx2EgBbIk3qomETIc3oaeh4aoArImAjKxl4CBJQSBhSpdmjYuvwUWg5dmazlc7k8Pm6sRC5HC4L4-AS-FQQA

💻 Code
class Foo {
    #value = 1234;
    getValue() { return this.#value; }
}

const foo = new Foo();

console.log(`foo.getValue() = ${foo.getValue()}`);

// Oops, I didn't bind!
const getValue = foo.getValue;

// Throws: Cannot read private member from an object whose class did not declare it
console.log(`getValue() = ${getValue()}`)

🙁 Actual behavior

Throws Cannot read private member from an object whose class did not declare it.

But the actual problem is that this is undefined and I had no idea!

🙂 Expected behavior

A special error for this when undefined; IIRC there's no possible way this would normally pass, so a special error would be a lot more clear.

Additional information about the issue

No response

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

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

はじめの一歩

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

調査の方向性

まず、リンクされた TypeScript Playground で束縛されていない private-field へのアクセスを再現し、issue に示されている束縛された呼び出しと比較します。コンパイラーが生成した private-field ヘルパーがどのように失敗を報告するかを追跡し、次に undefined-this のケースで説明されている、より明確なエラーが発生するようにして、両方の例を検証します。

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

評価

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

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

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