-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
Fixes #351, define prototype field on Class type. #632
Conversation
Can you apply it to |
Having just discovered That said, I think abstract-class.ts isn't really testing |
I've been thinking about interface AbstractClassAsInterface<T, Arguments extends unknown[] = any[]> {
prototype: T;
abstract new(...args: Arguments): T
// 'abstract' modifier cannot appear on a type member.(1070)
};
type AbstractClass<T, Arguments extends unknown[] = any[]> = {
prototype: T;
abstract new(...args: Arguments): T
// 'abstract' modifier cannot appear on a type member.(1070)
};
I recognize it's only 3 months old. I recommend removing |
I don't really see the need to remove the type. From what I understand, the |
I guess I'm tagged on this? If I were trying to write type _AC<T, A extends unknown[]> = abstract new (...args: A) => T;
interface AbstractClass<T, Arguments extends unknown[] = any[]> extends _AC<T, Arguments> {
prototype: T;
};
type Test = AbstractClass<Date>["prototype"];
// type Test = Date
const d: AbstractClass<Date> = Date; // okay
new d(); // Cannot create an instance of an abstract class. |
We (or at least, I) still haven't nailed this corrected type yet. I didn't test for constructor parameters in the class type. The following test fails in the playground. type Class<T, Arguments extends unknown[] = any[]> = {
new (...args: Arguments): T;
prototype: T;
};
type ClassWithArgs = Class<{counter: number}, [number]>;
const MyClassWithArgs: ClassWithArgs = class {
counter: number;
// @ts-expect-error I expect this to require a single argument
constructor() {
this.counter = 7;
}
} What am I missing here? I also tried jcalz's second variation. EDIT Here's what I missed. The constructor parameters are type-checked when we try to construct an instance: type A = [number] extends unknown[] ? true : false;
// ^?
type Class<T> = {
new (p: number): T;
prototype: T;
};
class E {
constructor() {
}
};
const B: Class<object> = E;
type C = ConstructorParameters<typeof B>;
// type C = [number]
type D = ConstructorParameters<typeof E>;
// type D = any[]
// @ts-expect-error for not having a parameter
const F = new B();
const G = new B(7); |
Yes, I know this is potentially risky, changing one of the most fundamental types in type-fest. I was unable to apply the same fix to
AbstractClass
.Still, I have added a test for the prototype field which fails on the existing type and passes with the new type.
Credit to @jcalz for the corrected type itself, at microsoft/TypeScript#54585.
Fixes #351