microsoft / microsoft/TypeScript

Support mixins for abstract classes

Offen
#35,356 16 Kommentare 12 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Search Terms

Related to #32122, but not the same.
abstract mixins classes

Suggestion

Right now we can't to extending abstract classes with mixins. So, it would be nice to create a solution for this case.

Use Cases

abstract class Test {
}

type Constructor = new (...args: any[]) => any;

function MyAwesomeMixin<T extends Constructor>(Constructor: Constructor) {
  abstract class MyAwesomeMixin extends Constructor {
  }
  return MyAwesomeMixin;
}

class A extends MyAwesomeMixin(Test) {
  /* Argument of type 'typeof Test' is not assignable to parameter of type 'Constructor'.
  Cannot assign an abstract constructor type to a non-abstract constructor type.(2345) */
}

Okay, try to create abstract constructor type:

type Constructor = abstract new (...args: any[]) => any;
/* Syntax error */

Hmm,..

Examples

Here is two way to create solution for this problem:

  1. (simple) Allow create abstract constructor types
type Constructor = (new (...args: any[]) => any) | (abstract new (...args: any[]) => any);
  1. (complex, but beautiful) Add new syntax to define mixins, and initially add support to extending abstract classes, like this:
abstract class Test {
   abstract mainMethod(): void;
}

mixin MyAwesomeMixin {
   public someMethod(): void {
      console.log('hello');
   }
}

class A extends MyAwesomeMixin(Test) {
   public mainMethod(): void {
      this.someMethod();
   }
}

// AND

abstract mixin MyAwesomeAbstractMixin {
    abstract someMethod(): void;
}

class B extends MyAwesomeAbstractMixin(Test) {
   public mainMethod(): void {
      this.someMethod();
   }
}

// Also this can add support to extending mixins without Mixin(Mixin2(Mixin3(Class)))

mixin MyAwesomeExtendedMixin extends MyAwesomeAbstractMixin {
   public someMethod(): void {
      ...
   }
}

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 abstract-class- und mixin-Beispielen im Issue und prüfe anschließend das verwandte Issue #32122. Vergleiche die vorgeschlagenen constructor-type- und mixin-syntax-Ansätze und bestimme, welches Design das Erweitern abstrakter Klassen unterstützt und dabei die genannten TypeScript-Designbeschränkungen beibehält. Als abgeschlossen gilt die Aufgabe, wenn das ausgewählte Verhalten spezifiziert ist und die Beispiele nicht mehr den gemeldeten constructor-type-Fehler erzeugen.

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.