microsoft / microsoft/TypeScript
Allow mixin constructor have parameters before rest parameter
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
const mixin1 = (base) => class Mixin1 extends base {
constructor(m1v1, m1v2, ...args) {
super(...args);
this.m1v1 = m1v1;
this.m1v2 = m1v2;
}
};
const mixin2 = (base) => class Mixin2 extends base {
constructor(m2v1, m2v2, ...args) {
super(...args);
this.m2v1 = m2v1;
this.m2v2 = m2v2;
}
};
class Base {
constructor(v1, v2) {
this.v1 = v1;
this.v2 = v2;
}
}
class C1 extends mixin1(Base) {
}
class C2 extends mixin2(Base) {
}
class C3 extends mixin2(mixin1(Base)) {
}
console.log(new C1("m1v1", true, 1, {}));
console.log(new C2(false, 2, 1, {}));
console.log(new C3(false, 2, "m1v1", true, 1, {}));
Examples
Same code expected work in ts with constructor inference.
type Constructor = new (...args: any[]) => object;
const mixin1 = <T extends Constructor>(base: T) =>
class Mixin1 extends base {
m1v1: string;
m1v2: boolean;
constructor(m1v1: string, m1v2: boolean, ...args: any[]) {
super(...args);
this.m1v1 = m1v1;
this.m1v2 = m1v2;
}
}
const mixin2 = <T extends Constructor>(base: T) =>
class Mixin2 extends base {
m2v1: boolean;
m2v2: number;
constructor(m2v1: boolean, m2v2: number, ...args: any[]) {
super(...args);
this.m2v1 = m2v1;
this.m2v2 = m2v2;
}
}
class Base {
v1: number;
v2: object;
constructor(v1: number, v2: object) {
this.v1 = v1;
this.v2 = v2;
}
}
class C1 extends mixin1(Base) {
}
class C2 extends mixin2(Base) {
}
class C3 extends mixin2(mixin1(Base)) {
}
console.log(new C1("m1v1", true, 1, { })); // infer mixin1 and Base parameters: [string, boolean, number, object]
console.log(new C2(false, 2, 1, { })); // infer mixin2 and Base parameters: [boolean, number, number, object]
console.log(new C3(false, 2, "m1v1", true, 1, { })); // infer mixin2, mixin1 and Base parameters: [boolean, number, string, boolean, number, object]
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files or tests are named. Start by locating the compiler logic that enforces mixin constructor constraints and infers constructor parameters, then trace how nested mixins are represented. Use the C1, C2, and C3 examples as regression cases and consider the work done when their constructor argument combinations are inferred as shown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100