Disallow getter that references itself
See original GitHub issueSuggestion
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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Would this make more sense (and be reasonably implementable) as a linter rule?
This example could be avoided by only raising an error when the recursive call was not inside a conditional.
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.