microsoft / microsoft/TypeScript

Support mixins for abstract classes

Aperta
#35,356 16 commenti 12 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi di abstract-class e mixin nell'issue, quindi esamina l'issue correlata #32122. Confronta gli approcci proposti di constructor-type e mixin-syntax e determina quale design supporta l'estensione delle classi astratte preservando i vincoli di progettazione di TypeScript indicati. Il lavoro è completato quando il comportamento scelto è specificato e gli esempi non producono più l'errore di constructor-type segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.