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.

Suggestion: readonly method

See original GitHub issue

Problem:

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:open
  • Created 6 years ago
  • Reactions:48
  • Comments:8

github_iconTop GitHub Comments

12reactions
feeddageekcommented, Jun 27, 2018

I’d like to add the the Readonly mapped type currently work on method event if the readonly keyword it self is not allowed.

class SomeClass {
    //public readonly SomeMethod() {} // 'readonly' modifier can only appear on a property declaration or index signature.
    public readonly SomeOtherMethod() {}
}
const someObject =  new SomeClass();
someObject.SomeOtherMethod = () => {}; // OK
const someOtherObject = new SomeClass() as Readonly<typeof SomeClass>
someOtherObject.SomeOtherMethod = () => {}; // Cannot assign to 'SomeOtherMethod ' because it is a constant or a read-only property.

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.

0reactions
adrian-gierakowskicommented, Nov 23, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

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