Suggestion: readonly method
See original GitHub issueProblem:
A method (function written with the ES6 method syntax) can currently be replaced with another function by a simple assignment.
As an example consider the following snippet of code:
class Person {
constructor(...) {...}
displayName (): string {...}
}
const x: Person = new Person(...)
x.displayName = function(this: Person): string {...} // Assignment
Because the readonly
modifier is not usable for methods, this is not possible to prevent this kind of assignments.
Proposal:
A method is always readonly.
The following codes are identical:
class Person {
displayName (): string {...}
}
class Person {
readonly displayName: (this: Person) => string = function () {...}
}
Compatibility:
This is a breaking change. However, method syntax is recent and mostly used in classes. Codes which assign a function to a method are certainly rare.
Temporary workaround:
Do not use method syntax in your classes and interfaces. Note hat this leads to very verbose codes.
If you use an interface, the verbosity is acceptable. However you get also strict variance.
interface PersonI {
readonly displayName: (this: Person) => string
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:48
- Comments:8
Top Results From Across the Web
Why does resharper suggest readonly fields - Stack Overflow
When a reference type is declared as readonly, the pointer is immutable, but not the object it points to. This means that:.
Read more >Readonly Method Level Variables in C# .NET - Tengiz Tutisani
Readonly method level variables are useful if you want to avoid accidental assignments within the method's body, as well as to clearly express...
Read more >Write safe and efficient C# code | Microsoft Learn
You add the in modifier to method signatures first. Then you can add the in modifier at call sites and create readonly struct...
Read more >Feature suggestion: Provide visual cues for readonly/const/non ...
... suggestion: Provide visual cues for readonly/const/non-mutating methods ... color or by doing further analysis to show that a method is non-mutating.
Read more >Suggestion for Permission Policy - ReadOnly access without ...
Thank you for your feedback. I passed it to our team. You can use the PermissionSettingHelper.AddTypePermissionsRecursively extension method to ...
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
I’d like to add the the Readonly mapped type currently work on method event if the readonly keyword it self is not allowed.
A flag under the strict umbrella to make all method readonly by default and force the use of property function for “assignable method” would also be welcome.
I’d really like to see this happen as currently using classes with one or more methods as a function argument lead to a linter error when https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/prefer-readonly-parameter-types.md is enabled.
It’s also quite inconsistent that a class property can be made immutable but a method cannot