Microsoft/TypeScript

Inability to use typeof on an ES private property

Open

#47,595 建立於 2022年1月25日

在 GitHub 查看
 (10 留言) (30 反應) (0 負責人)TypeScript (6,726 fork)batch import
Experience EnhancementHelp WantedSuggestion

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Bug Report

🔎 Search Terms

  • typeof
  • private

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about private and typeof

⏯ Playground Link

Playground link with relevant code

💻 Code

class Example {
  #privateProp = 'hello';

  constructor() {
    const p: typeof this.#privateProp = 'world';
    const p2: Example['#privateProp'] = 'world';
  }
}

The example is kept short for reproduction, but in practice this is more for cases where you would want to use keyof this.#someProperty and similar constructs (since here the type could just be inferred).

🙁 Actual behavior

Specifying a type using const name: typeof this.#anESprivateProperty is not possible.

For p, the error is that an identifier is expected (because it's a PrivateIdentifier and not an Identifier from the compiler's pov?).

For p2, the error is that the property does not exist on the type, which is logical when done from "outside" the class where the property should not be visible.

🙂 Expected behavior

Having the ability to use typeof on an ES private property from "inside" the class itself where that property is visible, e.g. like in the example code above.

貢獻者指南