Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inability to use typeof on an ES private property

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

RyanCavanaughcommented, Feb 1, 2022

For now I would not support indexed access lookup (const p: C["#p"] or variants thereof); it’s very messy. Accessing the type of a private property from outside the class is a huge code smell since it breaks under declaration emit, and should really be avoided.

typeof this.#p (from inside the class) should be the only way to get at it.

dnalborczykcommented, Jan 26, 2022

@RyanCavanaugh Do we need to support this syntax?

const p2: Example['#privateProp'] = 'world';

already in use with public properties.

class Example {
  #privateProp = 'hello';

  ['#publicProp']: number

type Prop = Example['#publicProp']

Read more comments on GitHub >

github_iconTop Results From Across the Web

Private and protected properties and methods
Privates should start with # . They are only accessible from inside the class. For instance, here's a private #waterLimit property and the...
Read more >
Why can I access TypeScript private members when I shouldn ...
Just as with the type checking, the privacy of members are only enforced within the compiler. A private property is implemented as a...
Read more >
Private Methods and Properties in TypeScript Classes
Learn how to use private methods and private properties in TypeScript and their benefits.
Read more >
Understanding and using interfaces in TypeScript
It assumes that measureTirePressure is a mandatory property and that it should be present in all of these models. However, when it doesn't...
Read more >
Part 45 - Government Property | Acquisition.GOV
(e) The cognizant contracting officer(s) may authorize the use of Government property on a rent-free basis on a cost type Government contract other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found