microsoft / microsoft/TypeScript

Support mixins for abstract classes

Open
#35,356 16 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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:

  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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the abstract-class and mixin examples in the issue, then review related issue #32122. Compare the proposed constructor-type and mixin-syntax approaches, and determine which design supports extending abstract classes while preserving the stated TypeScript design constraints. Done means the chosen behavior is specified and the examples no longer produce the reported constructor-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.