-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Intersection of constructor signatures #3916
Comments
What's a plausible implementation for a function that takes |
What do actual mixin libraries/implementations do here? |
@RyanCavanaugh and @danquirk - optionally you could create something that's a mixin style function: function mixin<E extends {new(a: number): any}, F extends {new(b: string): any}>(e: E, f: F) : E & F {
return class {
constructor(a: number, b: string) {
var result1 = new e(a);
var result2 = new f(b);
return merge(result1, result2);
}
}
} Notice that the merged constructor requires both parameters since it needs each one to construct the constituent parts. (obviously a full example would mix the static sides, too) |
Please change subject as this is more sum than intersection. |
I've thought about this issue more, and the current behavior actually seems correct in a way. If you think about the type of new (a: number): C;
new (b: string): D; This does have two overloads, and it should because if the type is simultaneously both C and D (the static sides), it is capable of constructing something in either of two ways. If you use the first construct signature, you get a C, and if you use the second, you get a D. I think we were confusing this with a different type, which would have been wrong, namely: new (a: number): C & D;
new (b: string): C & D; This type says that no matter which of the two options you choose for constructing the object, you will get something that is both a C and a D. This would indeed be wrong. Put differently, @jonathandturner used the word "requirement". I would consider construct signatures requirements with of the instance side of a class, but I would consider them capabilities of the static side of a class. I will close this, as I think the current behavior makes perfect sense. |
I'd say both assumptions are wrong:
You just don't know. |
Was just chatting with @JsonFreeman about how we might model mixins, and we came across an interesting snag.
Let's say we want to make something that represents the combination of two classes:
Class C has one requirement, a: number, and class D has another requirement, b: string. Currently what we do is to turn the construct signatures into an overload that takes either an a or a b. I'm not sure this makes sense, because in effect it's turning the requirements into optional parameters. You can't construct cd in a way that both a and b are valid.
Should parameters to the constructor, which act as its requirements for correct construction, instead be merged differently?
The text was updated successfully, but these errors were encountered: