-
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
Can't typecheck constructor in class generic parameter #23264
Comments
The problem is that an class Bar<Instance, Class extends HasOneParameterConstructor<Instance>> {}
class BarFooImpl extends Bar<Foo, typeof Foo> {} |
Hi @andy-ms , I tried your suggestion but the problem persists: export interface HasOneParameterConstructor<T> {
new (p1: any): T;
}
export abstract class ConstructorCheck<T, Class extends HasOneParameterConstructor<T>> {
}
export abstract class MyBase<TImplementor> extends ConstructorCheck<TImplementor, typeof TImplementor> {
}
class BaseImpl extends MyBase<BaseImpl> {
} Your example does work but it's hackable: class Hack { constructor(a: string) {} }
class BarFooImpl extends Bar<Foo, typeof Hack> {} I'm trying to create a library with some usage restriction, and that's not good. |
Note that TypeScript does not have any abstract static methods (#14600). It might be more helpful to determine what you're trying to accomplish -- currently |
Basically I want to stop the developer to define a new constructor for |
If I understand that right, you want something like this? abstract class A {
final constructor() {}
}
class B extends A {
constructor() { super(); someBadThing(); } // Compile error, super constructor is final
} It doesn't seem that useful to have an abstract class whose constructor can't be overridden -- sublcass constructors are forced to call the superclass constructor anyway. And one can of course do |
A final constructor would do the trick. |
This is a duplicate of #1534 then -- I don't think there's a good way to do what you want right now. (You can't just make the constructor private because then the class can't be overridden at all.) It's possible that you could add a run-time check in |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.8.1
Search Terms: generics constructor type check constraint new
Code
Expected behavior:
No errors: constructor type checking works
Actual behavior:
error TS2344: Type 'Foo' does not satisfy the constraint 'HasOneParameterConstructor'.
Type 'Foo' provides no match for the signature 'new (p1: any): Foo'.
Playground Link: Playground Test
Related Issues:
Maybe it's related to: Can't extend mixin constructor that constructs a generic type
The text was updated successfully, but these errors were encountered: