microsoft / microsoft/TypeScript
Allow Object.defineProperty() to definitely assign properties
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
Suggestion
✅ Viability Checklist
My suggestion meets these guidelines:
- 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 TypeScript's Design Goals.
⭐ Suggestion
It would be nice if Object.defineProperty() and Object.defineProperties() calls could be read via the constructor to definitely assign class properties.
📃 Motivating Example
class Greeter {
name: string;
greeting: string;
constructor(name: string) {
this.name = name;
Object.defineProperty(this, "greeting", {
get() {
return `hello ${name}`
}
});
}
}
Currently the greeting property in the class will cause the error Property 'greeting' has no initializer and is not definitely assigned in the constructor.
💻 Use Cases
This is not a big deal because we can use a “definite assignment assertion“ (!) after the greeting property identifier, but it would be nice for TypeScript to be aware of these defineProperty calls.
🔍 Search Terms
constructor, Object.defineProperty, Object.defineProperties, definitely assigned, strictPropertyInitialization, definite assignment operator
Property has no initializer and is not definitely assigned in the constructor.
This is potentially a duplicate of the following issues:
- https://github.com/microsoft/TypeScript/issues/28694 The same issue, but focused on JavaScript source files.
- https://github.com/microsoft/TypeScript/issues/38115 Might be a duplicate of this issue, but the phrasing of this issue is a little less clear to me based on the examples.
Feel free to close as duplicate!
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
Comienza ejecutando el ejemplo enlazado de TypeScript Playground y compara el error reportado de strictPropertyInitialization con la llamada a Object.defineProperty del constructor. Rastrea cómo se gestionan Object.defineProperty y Object.defineProperties durante la comprobación de tipos y define la semántica aceptada; el trabajo estará terminado cuando el ejemplo motivador se compruebe sin el error y el comportamiento existente permanezca sin cambios.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, 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
- 35/100