question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't extend mixin constructor that constructs a generic type

See original GitHub issue

This applies regardless of whether or not I intersect it with some bogus type, or have a reasonable constraint. So none of the following works.

Returning a plain T

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

// plain old T
export function Timestamp<T, CT extends Constructor<T>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Constructor of T with a bogus object type ({})

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

// Constructor of T with a bogus object type
export function Timestamp<T, CT extends Constructor<T & {}>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Constructor of T where T is constrained to {}

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

export function Timestamp<T extends {}, CT extends Constructor<T>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Constructor of T where T is constrained to object (see also #13805)

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

export function Timestamp<T extends object, CT extends Constructor<T>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
alber70gcommented, Feb 4, 2019

Anything to say about this? Is there a workaround possible? I’m anxious to hear about this, since we need it.

Another option could be that this is not proper coding, then I’d like to hear about it, as well.

0reactions
alber70gcommented, Jan 28, 2019

Any resolution on this?

I’m trying to do something similar where I want to use a class-mixin in another class-mixin.

A small example here:

class MyClass {
  myClass = 1;
}
type Constructor<T extends MyClass> = new (...args: any[]) => T;

function make1<T extends Constructor<MyClass>>(base: T) {
  return class extends base {
    make1 = 1;
  };
}
/**
 * [ts] Type '{
 *   new (...args: any[]): make3<T>.(Anonymous class);
 *   prototype: make3<any>.(Anonymous class); } & T'
 * is not a constructor function type. [2507]
 */
function make2<T extends Constructor<MyClass>>(base: T) {
  return class extends make1(base) {
    make2 = 2;
  };
}

class Test extends MyClass {}

class Test2 extends make2(Test) {
  constructor() {
    super();
    // this.myClass is gone
    // this.make1 is gone
    // only this.test2 exists
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript abstract class extend from mixin(class, generic ...
It seems that the compiler is widening the constructor to its constraint abstract new (...args: any) => object and getting its instance type, ......
Read more >
Documentation - Mixins - TypeScript
The pattern relies on using generics with class inheritance to extend a base class. TypeScript's best mixin support is done via the class...
Read more >
A First-Class Approach to Genericity - RICE CS
In this context, a mixin is simply a generic class that extends one of its type param- ... A class constructed as the...
Read more >
dart factory in abstract class - Lena's Italian Restaurant
Dart: doesn't override generic method has generic parameter type similar to the ... Can't both mixin and extend a class with a factory...
Read more >
Classes - JavaScript - MDN Web Docs
If there is a constructor present in the subclass, it needs to first call super() before using "this". One may also extend traditional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found