-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow mixin constructor have parameters before rest parameter #37143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
EDITED: Maybe more explanations. Let's consider the following set of classes as below 'Code Example 1' section. There are 3 holder classes that keeps string, number and Map. If we would like to have object that would keep name and tags, then we have to implement from scratch like in 'Code Example 2' section. This would be nice to use mixins for such ad hoc compositions. Lets have INeedCount and INeedTags as mixins and compose them like in 'Code Example 3' section. Easily extending existing classes with various constructors with multiple arguments or options using mixins, would be great!
|
If I understood. For this type of composition to be possible, the mixin should also know the parameters of the base class. Whether it could be implemented through a generic constraint. const INeedTagsMixin = <T extends new (options: INeedNameAndTagsOptions) => any>(base: T) =>
class INeedTagsMixin extends base {
tags: Map<string, string>;
constructor(options: INeedNameAndTagsOptions) {
super(options);
this.tags = new Map(options.tags);
}
} It would be an interesting addition. |
Not exactly. I would like to have option to specify more restricted constructor that "...args: any[]". The mixin from your example would have 2 generic arguments. The mixing class constructor would be intersection of required options' properties plus generic TOptions. Wrapping class with multiple mixins will look like onion, where each layer consume one "& TOptions" and pass rest further. Typically you will skin passed options with args and final ctor will get type that is defined with. I do hope it is rather clear what I mean - otherwise I could provide more extended example. interface INeedTagsOptions {
tags: ReadonlyArray<[string, string]>;
}
// I need to define that this mixin requires TOptions ctor arg to match below super call
const INeedTagsMixin = <T extends new (options: TOptions) => any, TOptions>(base: T) =>
class INeedTagsMixin extends base {
tags: Map<string, string>;
// here we need INeedTagsOptions to init self and whatever type of constructor arg when used
constructor(options: INeedTagsOptions & TOptions) {
super(options); // here we only need TOptions to satisfy "base" ctor
this.tags = new Map(options.tags);
}
}
class Class1 {
readonly x: number;
constructor(options: { x: number }) {
this.x = options.x;
}
}
class Class2 {
readonly name: string;
readonly ugly: boolean;
constructor(options: { name: string, ugliness: number }) {
this.name = options.name;
this.ugly = options.ugliness > 4;
}
}
const T1 = INeedTagsMixin(Class1);
const t1 = new T1({ x: "xx", tags: [["a", "aa"]] });
console.log(t1.tags.get("a"));
console.log(t1.x);
const T2 = INeedTagsMixin(Class2);
const t2 = new T2({ name: "zz", ugliness: 6, tags: [["a", "aa"]] });
console.log(t2.tags.get("a"));
console.log(t2.ugly); |
Search Terms
mixin, constructor parameters
Suggestion
Allow mixin constructor have parameters before rest parameter and infer the combination between mixin parameters.
Use Cases
The following js code is completely valid and functional. However with the actual mixin constructor constraints we can't reproduce this in Typescript.
Examples
Same code expected work in ts with constructor inference.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: