microsoft / microsoft/TypeScript
Suggestion: Add readonly references to objects & mark readonlyThis methods (pure functions)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Suggestion
🔍 Search Terms
- readonly method
- readonly this
- sealed class
- sealed this
- readonly reference #17181 (this issue is simmilar but more restrictive)
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code.
- It can or cannot be. It depends on wich name we choose
- 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Have a way to check readonly object references (including parameters, and this parameter). That allows to check than a function does not modifys an object.
📃 Motivating Example
class Figure{
constructor (public name: string){}
checkName(){
this.name += ' !'
}
readonlyMethod logAccess(){
console.log('one instance of Figure was accesed');
}
readonlyMethod safeCheck(){
// @ts-expect-error
this.name += ' x' // this is readonly in a readonlyMethod context
}
}
function showRO(figure: readonlyReference Figure){
console.log(figure.name);
// @ts-expect-error
figure.name+=" x"; // if figure is a readonly reference you can use it to modify the object
// @ts-expect-error
figure.checkName(); // if figure is a readonly reference you can use it to modify the object calling a method
figure.logAccess(); // ok logAccess is a readonly method
}
var x = new Figure('triangle');
showRO(x);
var z: readonlyReference Figure = x; // ok, you can assign a writeable object to a readonlyReference
// @ts-expect-error
var y: Figure = z ; // you cannot do the oposite.
💻 Use Cases
Actually I'am ussing Readonly aproach, but it not contemplates all cases.
I am trying with Object.seal and Proxyies .
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no implementation files or tests. Start by reviewing the proposal, linked issue #17181, the TypeScript design goals, and the linked Playground example. Done would require an agreed, implementable design for readonly references and methods, along with identified compiler tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100