Skip to content

Readonly methods and readonly types #61690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
Perfectoff opened this issue May 12, 2025 · 4 comments
Closed
6 tasks done

Readonly methods and readonly types #61690

Perfectoff opened this issue May 12, 2025 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Perfectoff
Copy link

πŸ” Search Terms

readonly methods

βœ… Viability Checklist

⭐ Suggestion

I suggest using the readonly keyword for methods, indicating that they don't change the contents of the "this" object, but only receive data. Similar to const methods in C++. This will provide good security from possible errors, as well as better understanding of the code:

class A {
  private value = 10;

  getValue() readonly { // mark the method as readonly, prohibiting changing the contents of the object 
    this.value = 0;   // Error: can't modify readonly object
    this.setValue(0); // Error: can't call a mutating method on a readonly object
    return this.value;
  }
  setValue(val : number) {
    this.value = val;  // OK
  }
}

And the types themselves can also be marked as readonly, meaning that all their contents become readonly, and only readonly methods are available:

function foo(a : readonly A) {
  a.setValue(0); // Error: can't call a mutating method on a readonly object
  a.getValue();  // OK
} 

We already have such syntax for declaring readonly arrays. And it will become a universal declaration for any readonly objects.

And if the method does not change the contents of the class, but returns a reference to some internal object of the class, then we can overload the signature of this method in two versions (with and without readonly):

  getData() readonly : readonly number[];
  getData()          : number[];

It is also necessary to clarify that the readonly type means deep readonly of the entire nested structure of the object, not just the first-level properties. And if we need a different behavior for individual properties, we can overload the getters for them, just like in the example above.

As an additional option, we can use readonly for the declared interface or class as a whole, eliminating the need to write it for each method:

interface ReadonlySome readonly { // mark the interface as readonly
  getValue() : number;  // becomes a readonly method
  calculate(data: readonly number[]) : number;  // becomes a readonly method
}

πŸ“ƒ Motivating Example

πŸ’» Use Cases

As I mentioned above, this concept is implemented in C++, but using the const keyword:

class A {
  int _value = 10;

  int getValue() const { 
    _value = 0;   // Error
    setValue(0); // Error
    return _value;
  }
  void setValue(int val) {
    _value = val; // OK
  }
  foo(const A& a) {
    a.setValue(0); // Error
  }
};

It is also partially implemented in C#, but in a very limited version. I don't see any reason why it couldn't be fully implemented.

In TypeScript as an alternative we can try to wrap "this" in some kind of typing:

class A {
  readonlyMethod(this : SomeReadonlyWrap<A>) { }
}

but I haven't found the right implementation of this wrapper yet. And anyway, it's very cumbersome to write it like that for all methods, especially in template classes.

@MartinJohns
Copy link
Contributor

Search Terms

readonly methods

Using these exact search terms I can easily find duplicates, e.g. #58236 or #22315.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 12, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2025
@Perfectoff
Copy link
Author

Perfectoff commented May 16, 2025

Search Terms

readonly methods

Using these exact search terms I can easily find duplicates, e.g. #58236 or #22315.

Well, your second link is not a duplicate, there is another topic, although its name is the same as mine, but the meaning is completely different (there, readonly means just preventing the replacement of a method by another)

And the first link suggests a similar meaning, but a different syntax, not very suitable for TS. But okay, we can continue there.

@MartinJohns
Copy link
Contributor

Well, your second link is not a duplicate, there is another topic, although its name is the same as mine, but the meaning is completely different (there, readonly means just preventing the replacement of a method by another)

True. My mistake, didn't look properly enough.

And the first link suggests a similar meaning, but a different syntax, not very suitable for TS. But okay, we can continue there.

Different syntax suggestion is not enough reason for a new issue. The problem that should be solved matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants