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.

Disallow getter that references itself

See original GitHub issue

Suggestion

There is never an instance (that I can think of) where a class getter function would need to reference itself. If it does so, attempting to call that function throws a too much recursion error. So the TypeScript compiler should not allow this to happen.

Use Cases

This would ensure that anyone who uses a private class member and adds a publicly accessible getter doesn’t accidentally reference the public getter instead of the private member (see example below).

It also ensures that, if someone is refactoring and wants to change a private member to a public one, they can use “Rename Symbol” to change, say _example to example and any existing get example() would raise an error.

Examples

The following would throw an error:

class Example {
  private _exampleProperty: string = "example";

  public get exampleProperty(): string {
    return this.exampleProperty; // Self-referential; should be this._exampleProperty
  }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
    • I don’t know about this? It might raise errors where errors don’t exist currently but I can’t think of an example where this shouldn’t be an error.
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.
    • This aligns with goal 1: “Statically identify constructs that are likely to be errors.”

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
nmaincommented, Aug 5, 2019

Would this make more sense (and be reasonably implementable) as a linter rule?

Getters can behave like recursive functions. I can’t come up with a non-silly example, but maybe someone has:

This example could be avoided by only raising an error when the recursive call was not inside a conditional.

0reactions
fatcerberuscommented, Aug 5, 2019

I guess the tricky thing is that a getter is a nullary function so you can’t encode the termination condition without reference to some external data (global variable, instance property, etc.). A getter could be recursive in this sense, but it certainly wouldn’t be my first choice of how to implement one.

Definitely seems to be more in the domain of a lint rule than a tsc error, at any rate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c++ prevent changing reference through getter - Stack Overflow
Basically it want to prevent b.getA() = anotherA; being allowed. Is this possible in c++ or is my design completely wrong here? This...
Read more >
Avoid getters and setters whenever possible
If you only have a getter, things can be just as bad. In Java at least, returning a reference type from a getter...
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
The get syntax binds an object property to a function that will be called when that property is looked up. It can also...
Read more >
getter and setter in Swift (from ObjC) - Apple Developer
I get an infinite loop on get as self.image calls itself repeatidly (why not the ... In fact my code is pure Swift,...
Read more >
Getters and Setters: Manage Attributes in Python
The Pythonic way to attach behavior to an attribute is to turn the attribute itself into a property. Properties pack together methods for ......
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