microsoft / microsoft/TypeScript
Property initializers output for non-initialized types
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
Bug Report
This is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to provide a solution for people facing the same.
Issue Summary
- ESNext spec seems to call for defining all class fields. This also affects those which are optional or specified with a boom.
- Spec also dictates that property initializers are called after the super call. (https://github.com/tc39/proposal-class-fields)
This results in any properties specified in the inherited class that are assigned during the base class constructor to be overwritten, even if they do not have initializer values specified in the inherited class.
The behaviour which causes the error can be seen below:
class A {
a?: string
}
Output is:
// target = ESNext
class A {
a; // Outputs a statement without initialized value, which is treated as a = undefined;
}
// target = ES2020
class A {
}
Here is a simplified version of how this affected me
abstract class Base<T extends Record<string, any>> {
// Single constructor for all derivatives
constructor(o: Omit<T, typeof Base>) {
Object.assign(this, o);
}
}
class A extends Base<A> {
myProp?: string
myProp2!: string
}
const a = new A({ myProp2: 'hello' });
// ESNext: a = { myProp2: undefined, myProp: undefined }
// ES2020: a = { myProp2: 'hello' }
The above produces:
// TypeScript target=ESNext
class A extends Base {
myProp;
myProp2; // Note the boomed property still gets output here, so it is initialized after the super call - which maybe surprising behaviour
}
// ES2020 / Babel
class A extends Base {
}
🔎 Search Terms
- property initializers
🕗 Version & Regression Information
TS 4.3.5
Solution
For those facing this issue, simply change property declarations to ambient.
ie:
class A extends Base<A> {
declare myProp?: string
declare myProp2: string
}
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 con los ejemplos mínimos de clases del issue y compara la salida emitida para los destinos ESNext y ES2020, incluidas las propiedades opcionales y las propiedades con asignación definitiva. Determina si el resultado previsto es un cambio en el compilador o documentación, y verifica después que el comportamiento elegido no sobrescriba los valores asignados por el constructor base.
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
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100