-
-
Notifications
You must be signed in to change notification settings - Fork 546
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
[Bug]: Class declaration is broken #696
Comments
// @ajvincent |
I'm not sure I buy this argument. There's a couple holes here, specifically around not specifying types explicitly. var a = new C(B); You haven't specified the type argument for C, and there's no default type there. If I make one change: var a = new C<B<BVal>>(B); then I get the expected result. |
Clarification: I'm not saying I'm right. I'm just saying you haven't convinced me yet. |
I expect TS to infer the type from the argument here implicitly. And TS is capable of it: the type is correctly inferred when I replace the type of |
Yeah, looks like it really depends on the type name. If I make a simple type Copy<T> = {
[P in keyof T]: T[P];
}
export type Class<T, Arguments extends unknown[] = any[]> = {
prototype: Copy<T>;
new (...arguments_: Arguments): T;
};
class A<T = unknown> {
value: T;
constructor(value: T) {
this.value = value;
}
}
interface BVal {
field: number;
}
class B<T extends BVal = BVal> extends A<T> {
}
class C<T extends A> {
instance: T;
constructor(cls: Class<T>) {
this.instance = new cls({ field: 10 });
}
}
var a = new C(B); |
That is very interesting. I remember the keyof operator didn't quite capture everything. Specifically, that it excludes I think you've sold me at this point. Let me get back to you. I also think |
I found that the
Class
declaration is broken.In this example:
the type of
a
is notC<B<BVal>>
as I expected, butC<B<any>>
, which breaks the type inference.After some investigation I found that it was caused by using the
T
generic as a value for theprototype
property. It is incorrect because the prototype inconstructor
should be the parent class, not the instance of the declaring class. That's why TypeScript fails to infer the correct type.If I change
prototype
type toobject
,a
has the correct typeC<B<BVal>>
.Upvote & Fund
The text was updated successfully, but these errors were encountered: