question-mark
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.

Using `typeof this.#privateField` in declaration emit

See original GitHub issue
class StringHolder {
    #data = "hello";

    public getData(): typeof this.#data {
        return this.#data;
    }
}
error TS4055: Return type of public method from exported class has or is using private name 'this'.

8     public getData(): typeof this.#data {
                               ~~~~

Seems strange that we even have this feature if we can’t use this in declaration emit.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fatcerberuscommented, Apr 8, 2022

Why is it complaining about this rather than the actual thing that’s private?

0reactions
dvoytenkocommented, Sep 16, 2022

I’m seeing the same error even for simple cases like this:

class X {
  setValue(): typeof this {
    ...
   return this;
  }
}

It’s convenient to return typeof this in this case because it will also have the right type for the subclasses of X.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I access TypeScript private members when I shouldn ...
Private fields starts with # character ... My fix is to group private declarations together inside another class that is then instantiated in...
Read more >
typeof - JavaScript - MDN Web Docs - Mozilla
The typeof operator returns a string indicating the type of the operand's value.
Read more >
How to: Define a Generic Type with Reflection Emit - .NET ...
See how to define a generic type with reflection emit. ... In this example, the type of the field is specified by type...
Read more >
Private Methods and Properties in TypeScript Classes
We can prefix the constructor parameters with the public or private keyword to automatically have TypeScript assign the parameter as a property ...
Read more >
Documentation - Classes - TypeScript
As with other JavaScript language features, TypeScript adds type annotations and ... A field declaration creates a public writeable property on a class:....
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found