microsoft / microsoft/TypeScript

Class mixins shouldn't require a specific signature of constructors

Offen
#14,126 5 Kommentare 15 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: 2.2.0-dev.20170209

Code

Requiring mixins to have a constructor signature of constructor(...args: any[]) causes a few problems:

  1. Node v4, which is an active LTS, doesn't support rest and spread syntax. We avoid it and can run our output on Node v4 even when targeting ES6. The mixin constructor requirements prevent this.

  2. It's common to have mixins that want to provide a specific constructor. These will usually be applied last to create a concrete class. This is impossible now, so you have to create a new concrete class with only a constructor to give it the correct signature.

  3. For a mixin that does have constructor argument requirements, it's very cumbersome to extract arguments out of ...args.

Here's an approximation of a case I hit. I have two classes that that share a base class:

class Base {
  foo: string[];
}

class A extends Base {}

class B extends Base {}

Later, I want to add some functionality to via mixins to create two new classes:


type Constructor<T extends object> = new (...args: any[]) => T;
interface Magic {}
const Magic = <T extends Constructor<Base>>(superclass: T): Constructor<Magic>& T =>
  class extends superclass implements Magic {
    constructor(foo: string[]) {
      super();
      this.foo = foo;
    }
  };

const MagicA = Magic(A);
const MagicB = Magic(A);

And I want MagicA and MagicA to have a constructor that takes a single argument. I know the code will work because I know the constructors of A and B, but the type checker is unhappy.

First, the Constructor type is wrong in this case because I know the signature I want. I'd actually like to write:

type BaseConstructor = new() => Base;
type MagicConstructor = new(foo: string[]) => Magic;
interface Magic {}

const Magic = <T extends BaseConstructor>(superclass: T): MagicConstructor & T =>
  class extends superclass implements Magic {
    constructor(foo: string[]) {
      super();
      this.foo = foo;
    }
  };
const MagicA = Magic(A);
const MagicB = Magic(A);

Expected behavior:

This works

Actual behavior:

Error: "Type 'T' is not a constructor function type." on the extends clause.

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 damit, das TypeScript-2.2-Beispiel für ein Constructor-Mixin aus dem Issue zu reproduzieren, wobei du dich auf den Fehler "T is not a constructor function type" in der extends-Klausel konzentrierst. Untersuche, wie der Type Checker Mixin-Konstruktoren validiert; als erledigt gilt die Aufgabe, wenn das gezeigte Mixin seine spezifische Konstruktorsignatur beibehalten kann, ohne constructor(...args: any[]) zu erfordern.

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
35/100

Neue Issues direkt in Ihr Postfach

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