microsoft / microsoft/TypeScript
ConstructorParameters is not working for private constructors (and protected constructors)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
TypeScript Version: 3.4
Search Terms: ConstructorParameters, private constructor
Code
class Foo
{ public constructor(a: number, b: boolean, c: string) { } }
class Bar
{ private constructor(a: number, b: boolean, c: string) { } }
type FooCtorArgs = ConstructorParameters<typeof Foo>;
type BarCtorArgs = ConstructorParameters<typeof Bar>;
Expected behavior:
BarCtorArgs is [number, boolean, string].
Actual behavior:
Error in line 8:
Type 'typeof Bar' does not satisfy the constraint 'new (...args: any) => any'. Cannot assign a 'private' constructor type to a 'public' constructor type.
Use Case
Convenient factory pattern to mimic asynchronous object construction.
class Component
{
public static async create(...args: ConstructorParameters<typeof Component>)
{
const component = new Component(...args);
await component.initialize();
return component;
}
private constructor(a: number, b: boolean, c: string)
{ /* stuff */ }
private async initialize()
{ /* asynchronous setup */ }
}
Ideas for Solution
Allow visibility modifiers in interfaces. Yes, if you use an interface to outline the visible properties of an object to a user this makes no sense, but why not allow interfaces to be a flexible tool to specify internal rules of implementations? I think this problem could be solved, if the ConstructorParameters type could be defined like the following:
type ConstructorParameters<T extends {private new (...args: any[]): any}> = T extends new (...args: infer P) => any ? P : never;
Which is of course no allowed at the moment.
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
Beginnen Sie mit der verlinkten Playground-Reproduktion und dem Verhalten von TypeScript 3.4 für ConstructorParameters, angewendet auf private und protected-Konstruktoren. Identifizieren Sie die relevanten Einstiegspunkte für Compiler und Regressionstests und stellen Sie anschließend ein Verhalten her, das das Tupel der Konstruktorargumente ableitet, ohne die Sichtbarkeitsprüfungen zu lockern; abgeschlossen ist die Arbeit, wenn das Beispiel mit dem erwarteten Tupel typgeprüft wird und die Testabdeckung Regressionen verhindert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100