-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
(Vanilla) Mixins Pattern: Base constructor must have same return type #40110
Comments
I got the same error in the Beta version of WebCell 2.3. If I set the value type of Sub-class Constructor same as its super, the error disappears, and the Sub-class properties disappear in Code Hinting, too... |
This is still a major problem for us, as our component library is meant to be extended by subclassers, and we use mixins basically everywhere. This means we cannot possibly strongly-type our library and our users can't use our library with Typescript, because the I see that this was added for 4.1.0 milestone but we're already at 4.2.0. Will this issue be picked up still? |
This isn't a bug. The compiler is saying that the declare type Constructor<T> = new (...args: any[]) => T;
declare class FooHost {
static foo: string;
}
declare function FooMixin<T extends Constructor<HTMLElement>>(superclass: T): T
& Constructor<FooHost>
& Pick<typeof FooHost, keyof typeof FooHost>
class Foo extends FooMixin(HTMLElement) { } |
You can also fix this by giving declare type Constructor<T> = new (...args: any[]) => T;
declare class FooHost {
static foo: string;
constructor(...args: any[]);
}
declare function FooMixin<T extends Constructor<HTMLElement>>(superclass: T): T
& Constructor<FooHost>
& typeof FooHost
class Foo extends FooMixin(HTMLElement) { } |
That makes sense, thanks a lot for explaining and suggesting a fix! |
I'm sorry but can we reopen this as there was really no explanation as to why is this considered "not a bug" and why constructors were "considered" incompatible if they are actually compatible (there are no conflicting props/methods etc.)? I'm trying to build a form associated mixin for web components with full type safety but this bug is basically destroying typing system. Basically TS complains at the time I'm using (extending) my mixin with error "Base constructors must all have the same return type" even though technically they are compatible. Also worth noting that I've specifically avoided "mixin-style constructor signature" as in this particular case it's not allowed to pass any constructor arguments as this are web components which have strictly no arguments by design, so having |
I tend to agree that As for #36821 this issue is somewhat related I think because it also is a case of where TypeScript is pretty constraining/conservative towards Mixin patterns, and the suggestion here is that when this is the case, and the code is not objectively wrong, The biggest issue with this escape-hatch is that it means that your consumers must put |
I came across this because I used a function to modify the prototype of a class: set<T1 extends typeof ICustomElement, T2 extends object>
(this: T1, source: T2 & ThisType<T2 & InstanceType<T1>>, options?: Partial<PropertyDescriptor>)
: T1 & (new (...args: ConstructorParameters<T1>) => InstanceType<T1> & T2) Basically to do something like I found out the problem is that the above type will not override the constructor, but overload it. That means calling new (): (...args: ConstructorParameters<typeof ListItem>) => ListItem;
new (): (...args: ConstructorParameters<typeof ListItem>) => ListItem & { type: 'menu'} ; It makes sense because set<T1 extends typeof ICustomElement, T2 extends object>
(this: T1, source: T2 & ThisType<T2 & InstanceType<T1>>, options?: Partial<PropertyDescriptor>)
: {[P in keyof T1]: T1[P]} & (new (...args: ConstructorParameters<T1>) => InstanceType<T1> & T2) It's technically not a bug, just one of the quirks of trying to override a A utility type (eg:
|
TypeScript Version: 4.1.0-dev.20200818
Search Terms: Base constructors same return type
Code
The reason I do
& typeof FooHost
at the end of FooMixin is because I need to be able to access the static propertyfoo
of the FooMixin's host class. This part works. But it creates a new error:What is important here to note is that in my project I use
allowJs
+checkJs
so I have to find a way that doesn't include me being forced to do something like:Because I can't, my implementation is in JavaScript (where the FooMixin is applied).
Expected behavior:
No error
Actual behavior:
Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-dev.20200818#code/CYUwxgNghgTiAEAXAngBwQYQPYDsDOiMArmIljADwAqAfPALzw4gDu8AFAHTewDmeALnhQcyANoBdAJQM6VANwAoRaEiwEavHngAxLFgASWAvADei+PAJREASzBWwWdMACiEEAFsQORIKuEtji8SgC+yqrQcPAAZkQ4pLa4uvoAsrYAHkHU8CAZiD7A2tj4hCRklAZUqQAy7l4+iDQ07HhE6DCa-lRSQlTwAGTwJQTEpOQUeobGTYNIaCBYMSnTBMqKXSu5+YXaU+lZOOxVtfXevjKm8KFAA
The text was updated successfully, but these errors were encountered: