Can't extend mixin constructor that constructs a generic type
See original GitHub issueThis 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:
- Created 7 years ago
- Reactions:10
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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: