-
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
JSDoc @overload
s on constructors don't capture class type parameters, can dictate their return type
#55919
Comments
I'll point out that in TS itself, construct signatures can't add more generic type parameters than their containing class. It's an irregularity that overload signatures permit them in the JSDoc version. |
Some extra notes on this topic:
Some thoughts on generic constructors: If people need to make sure that the generic type parameters have distinct constraints for each overload (which is my use case), then the workaround syntax might actually be desirable. Technically speaking, it's the constructor that's generic in each use case rather than the class itself. There are times when the constructor needs to behave differently depending on its inputs, but the constructor will produce the same API as an output regardless. In these cases, the class is technically not generic. I know generic constructors aren't currently supported in TypeScript, but I think #40451 gives a compelling case for their use. They're technically already doable with I wasn't sure if any of these comments needed to be split out into separate issues. If they do, please let me know and I can split them out. |
(1) and (2) need to be tested in the fix for this bug. Your thoughts on generic constructors would be good to have on #40451; JS isn't supposed to support generic constructors until TS does. |
Added a comment to that issue just in case. I have time to write comments down now so I figured I'd just take advantage of the opportunity if that was the desire. 😅 |
Oh, I meant it would be good to write down your thoughts from here, on #40451. Basically exactly what you did; sorry I was unclear. |
I know I linked it in the other issue, but just in case the reference to my use case is needed here as well: https://github.com/enthusiastic-js/form-observer/blob/main/packages/core/FormObserver.js |
Because TypeScript does not yet support generic constructors for JSDocs, this change is necessary in order to guarantee a decent user experience for the users of `@form-observer/core`. See: - microsoft/TypeScript#55919 - microsoft/TypeScript#40451
Found from #55916.
Today if you try to write a generic constructor, captured type parameters fail to create a generic signature. Instead, the type parameter is effectively leaked and never instantiated.
The current-workaround is to write something like the following:
Notice that here, each
@overload
must declare its own type parameters and return type.We should decide if this is actually how we want
@overload
to work in JSDoc onconstructor
declarations.The text was updated successfully, but these errors were encountered: