-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Allow type annotations on constructors #13355
Comments
@aluanhaddad the problem is not with the type annotation on the person declaration...I see where you're going with that, but that's not the issue. The problem is that I'm constructing a new object of type Say for example,
Therefore, I need to be able to annotate the constructor to make it clear to typescript that the constructor generates Essentially this boils down to the fact that I can annotate new in interfaces but not constructor in classes.. This works...
This doesn't...
Possible solutions I can see...
|
Duplicate of #4890? |
I want to add type annotation to constructor too.
|
@vvakame see #11588 (comment) for the rationale behind not allowing this. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I have a particular case (very possibly edge case) where I want to define a class that takes a template, and merges the template into the classes object instance, thus, the instance would be an intersection of my template and the class itself.
My current code works but has some limitations:
View in the Playground
The limitations here are that because I can't define a constructor that returns an intersection type, I cannot define a class at all, therefore I lose the ability to use TypeScript's inheritance. This is because I have to implement the functionality exposed by these interfaces manually, using JavaScript.
Therefore I would like TypeScript to allow type annotations on constructors (perhaps the limitation here should be that the annotation should be an intersection type which includes the class)
In conclusion, I could therefore Implement the class correctly, allowing the class to use inheritance.
As another point related to this, the current declaration model in TypeScript needs revising, because it makes it appear that you can construct interfaces that have no implementation. Consider this from lib.d.ts
Object at this point appears like an interface, but can be instantiated and not inherited. This is not natural in OOP. It would be better if the declaration could be...
The text was updated successfully, but these errors were encountered: