microsoft / microsoft/TypeScript
Suggestion: Add readonly references to objects & mark readonlyThis methods (pure functions)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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 .
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Die Issue nennt keine Implementierungsdateien oder Tests. Beginne mit der Durchsicht des Vorschlags, des verlinkten Issues #17181, der TypeScript-Designziele und des verlinkten Playground-Beispiels. Erledigt wäre die Aufgabe, wenn ein abgestimmtes, implementierbares Design für readonly-Referenzen und -Methoden sowie identifizierte Compiler-Tests vorliegen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100