microsoft / microsoft/TypeScript
Allow `readonly` with `accessor`
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔍 Search Terms
accessor
decorator
readonly
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- 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 our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Add readonly as a modifier for accessor fields.
📃 Motivating Example
Suppose you have an injection framework with @provides, @consumes, and @inject. Consider the example
class A {
@provides(Number)
readonly value: number;
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.value = value;
}
}
class B {
@consumes(Number)
readonly value: number;
}
Note because @provides is a field decorator, it cannot know when value is assigned unless it's in the initializer. As a consequence, b will not have value injected since @provides doesn't know when to reinject. This is fixed if you do
class A {
@provides(Number)
readonly accessor value: number;
@inject([Number])
accessor b = new B();
constructor(value: number) {
this.value = value;
}
}
class B {
@consumes(Number)
readonly value: number;
}
💻 Use Cases
- What do you want to use this for?
Accessor declarations that shouldn't be modified after being used in the constructor. - What shortcomings exist with current approaches?
You can only mark a field with@readonlyusing JSDoc and just hope that other developers don't touch the field. - What workarounds are you using in the meantime?
@readonlyusing JSDoc
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
En el issue no se identifican archivos de código fuente, pruebas ni puntos de entrada de la implementación. Empieza rastreando cómo se analizan y comprueban las declaraciones de accessors y los modificadores readonly; después, añade cobertura que demuestre que las declaraciones de accessors readonly se aceptan y no se les puede reasignar un valor.
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
- Necesita aclaración
- Aptitud para principiantes
- 35/100