microsoft / microsoft/TypeScript
Allow static members in abstract classes to reference type parameters
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Search Terms
abstract generic class static type parameter
Suggestion
It has been previously concluded in #24018 that referencing class type parameters in the static side is problematic unless that class is meant to be extended. Well, abstract classes are meant to be extended, so it makes sense to allow type parameters to be used?
To quote @andy-ms in #24018:
Without inheritance that wouldn't make much sense:
class Super<T> { static m(x: T): void; } Super.m(); // What's `T`?
This is a valid point. But with a solution for abstract static members from #34516, this could be refactored like so:
abstract class Super<T> {
abstract static m(x: T): void;
}
class A extends Super<number> {
static m(x) {
console.log(x * 42);
}
}
class B extends Super<string> {
static m(x) {
console.log(x + ' World!');
}
}
A.m(2);
B.m('Hello');
Use Cases
Pretty much everywhere that instance side types are related to static side types and where instance methods depend on static properties.
Examples
As an example I'll give my personal use case. I have a class with a static defaults property and instances merge it with a constructor argument of the same type but partial. Then, the resulting object is stored in an instance property:
abstract class Base<T> {
static defaults: T
config: T
constructor(options: Partial<T>) {
this.config = Object.assign({}, (this.constructor as typeof Base).defaults, options);
}
}
interface Options {
a: string
b: number
}
class A extends Base<Options> {
static defaults = {
a: 42, // Type '42' is not assignable to type 'string'.
b: 'oops' // Type '"oops"' is not assignable to type 'number'.
};
}
let inst = new A({
a: 'bar', // OK
b: 'baz' // Type '"baz"' is not assignable to type 'number'.
});
inst.config.a = 12; // Type '12' is not assignable to type 'string'.
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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 leyendo la discusión en #24018 y la propuesta relacionada sobre miembros estáticos abstractos en #34516. No se nombran archivos del repositorio ni pruebas, así que localiza las rutas de comprobación de tipos para miembros estáticos y clases genéricas. Se considerará terminado cuando las clases genéricas abstractas puedan usar sus parámetros de tipo en el lado estático con una comprobación de tipos adecuada y cobertura de regresión.
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