microsoft / microsoft/TypeScript
Support mixins for abstract classes
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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:
- (simple) Allow create abstract constructor types
type Constructor = (new (...args: any[]) => any) | (abstract new (...args: any[]) => any);
- (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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les exemples de abstract-class et de mixin dans l’issue, puis examinez l’issue associée #32122. Comparez les approches proposées de constructor-type et de mixin-syntax, et déterminez quelle conception permet d’étendre des classes abstraites tout en respectant les contraintes de conception de TypeScript indiquées. Le travail est considéré comme terminé lorsque le comportement choisi est spécifié et que les exemples ne produisent plus l’erreur de constructor-type signalée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100