microsoft / microsoft/TypeScript
ConstructorOf<T> wrapper to declare type of constructor of type parameter
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
so if I have
class MyClass {
}
Then the type of MyClass.prototype is MyClass
And the type of MyClass is typeof MyClass
If I want these two functions
function takesPrototype(p: MyClass) { /* ... */ }
function takesClass(p: typeof MyClass) { /* ... */ }
I can call them as
takesPrototype(MyClass.prototype)
takesClass(MyClass)
But what if MyClass was defined as a type parameter.
For example if I want to have a function that of this type -
const mod = getModule(ModuleClass)
// mod should be of type ModuleClass
I would like to define this as such
function getModule<M extends Module>(moduleClass: typeof M): M {
// something like a getInstance() pattern
}
The problem with this is typeof M will complain that I am trying to use M as a value while it is a type.
So I suggest a type wrapper of this type
type ConstructorOf<C> = { new( ...args: any[] ): C }
Now I can define my function as such
function getModule<M extends Module>(moduleClass: ConstructorOf<M>): M {
// something like a getInstance() pattern
}
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 der Überprüfung des im Issue vorgeschlagenen ConstructorOf-Wrappers und der bestehenden Unterscheidung zwischen Typparametern und Konstruktorwerten in TypeScript. Ermittle die beteiligten Einstiegspunkte im Typsystem und Compiler und definiere anschließend das unterstützte Verhalten für generische Konstruktoren. Validiere dies mit gezielten Typprüfungstests, die getModule und Konstruktorargumente abdecken.
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