Closed
Description
TypeScript Version: 2.8.1
Search Terms: generics constructor type check constraint new
Code
export interface HasOneParameterConstructor<T> {
new (p1: any): T;
}
class Foo {
constructor(p1: string) {
};
}
class Bar<T extends HasOneParameterConstructor<T>> {
}
class BarFooImpl extends Bar<Foo> { // error here under "Foo"
}
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
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]Can't typecheck constructor in generic parameter[/-][+]Can't typecheck constructor in class generic parameter[/+]ghost commentedon Apr 9, 2018
The problem is that an
Foo
is notHasOneParameterConstructor
because you can't callnew
on an instance ofFoo
. You need to have separate types for the instance and class type.albyrock87 commentedon Apr 10, 2018
Hi @Andy-MS , I tried your suggestion but the problem persists:
Your example does work but it's hackable:
I'm trying to create a library with some usage restriction, and that's not good.
ghost commentedon Apr 10, 2018
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
Class
is unused there. Note that the type parameter is only available as a type, and not as a value, so you won't be able to writenew T
inside of your class body, and as you've noticedtypeof T
doesn't work.albyrock87 commentedon Apr 14, 2018
Basically I want to stop the developer to define a new constructor for
BaseImpl
. This way I can create new Instances ofBaseImpl
using exactly the MyBase constructor.ghost commentedon Apr 16, 2018
If I understand that right, you want something like this?
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
new B(); b.someBadThing();
even if the constructor isn't overridden.albyrock87 commentedon Apr 16, 2018
A final constructor would do the trick.
I'm in the Angular world, and I'm trying to stop the developer to
@Inject
other things in theB
class.ghost commentedon Apr 16, 2018
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
A
's constructor thatthis.constructor
has no decorators? I don't really know how angular works though.typescript-bot commentedon Apr 30, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.