microsoft / microsoft/TypeScript

"Pick" or "Exclude" constructor from class type

Offen
#29,261 9 Kommentare 10 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Search Terms

intersect, type, intersect constructor, pick new, pick constructor

Suggestion

class C1 {
  static S1 = 'foo';
  bar = 'bar';
  constructor(p1, p2, p3) {}
}
type T1 = Pick<typeof C1, new>;
// could simply be also:
type Only<T, K extends keyof T> = Pick<T, { [P in keyof T]: P extends K ? P : never }[keyof T]>;
type T2 = Only<typeof C1, 'constructor'>;

const clazz: T1 = null; // `null` could be anything
new clazz(); // should throw error because of missing params
new clazz(null, null, null); // should work
new clazz(null, null, null).bar; // should throw error because `bar` doesn't exist
clazz.S1; // should throw error because `S1` doesn't exist

Use Cases

It could be useful in class type intersection and/or for multiple inheritance (like PHP Trait, or Java default methods in interface).
And make mixins more "clean" imho.
One of my goal is to make a "type mixin" without a "value mixin".

Examples

For example:

class C1 {
    public bar: string;
    constructor(p1: string) {}
    public foo() {}
}

class C2 {
    public constructor() {}
    public baz() {}
}

type T1 = typeof C1 & typeof C2;

const a: T1 = null;
a.prototype.foo(); // ok
a.prototype.bar; // ok
a.prototype.baz(); // ok
new a(); // ok (return C2)
new a(''); // ok (return C1)
new a().foo() // error
new a('').baz(); // error

type T2 = typeof C1 & Exclude<typeof C2, new>

const b: T2 = null;
b.prototype.foo(); // ok
b.prototype.bar; // ok
b.prototype.baz(); // ok
new a(); // error <<<
new a(''); // ok (return C1 with C2 prototype)
new a('').foo() // ok (from C1)
new a('').baz(); // ok (from C2) <<<

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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den Typalias- und Konstruktor-Intersection-Beispielen im Issue und verfolge dann, wie TypeScript Konstruktor- und Instanztypen darstellt. Definiere das beabsichtigte Verhalten für das Auswählen oder Ausschließen von Konstruktoren, einschließlich der gezeigten gültigen und ungültigen Aufrufe sowie der Property-Zugriffe. Als abgeschlossen gilt die Aufgabe, wenn die Beispiele die Typprüfung bestehen oder wie angegeben einen Fehler erzeugen, mit Abdeckung für das vorgeschlagene Verhalten.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.