microsoft / microsoft/TypeScript
Allow static members in abstract classes to reference type parameters
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Lesen der Diskussion in #24018 und des zugehörigen Vorschlags zu abstrakten statischen Membern in #34516. Es werden keine Repository-Dateien oder Tests genannt; finde daher die Pfade für die Typprüfung statischer Member und generischer Klassen. Als erledigt gilt die Aufgabe, wenn abstrakte generische Klassen ihre Typparameter auf der statischen Seite mit geeigneter Typprüfung und Regressionstests verwenden können.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100