microsoft / microsoft/TypeScript
Suggestion: Add readonly references to objects & mark readonlyThis methods (pure functions)
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
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 .
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
El issue no menciona archivos de implementación ni tests. Empieza revisando la propuesta, el issue enlazado #17181, los objetivos de diseño de TypeScript y el ejemplo enlazado de Playground. El trabajo estaría terminado cuando exista un diseño acordado e implementable para referencias y métodos readonly, junto con tests del compilador identificados.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100