-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Inferred type incomplete, when inferring parameters type on surcharged constructors. #59812
Comments
You didnβt provide any search terms. Did you search for existing issues before filing this? Also, βsurchargedβ isnβt a term that makes much sense here but from context I gather you mean βoverloadedβ. Duplicate #43731 |
This is also in the documentation: Inferring Within Conditional Types
|
Sorry for that, I was in a train with very slow/limited Internet access.
Indeed, bad translation (in French we say "surcharge").
So according to the documentation :
The last signature being : function foo(a: boolean|number) Shouldn't |
"I was in a train" So, um, wait until you're off the train to file issues? Maybe? I don't know how to process this response. "The last signature being" no, that's the implementation. It's not a call signature (try calling the function with |
Well, I don't want this to be a call signature, but the implementation signature to be used during inferences as it makes the most sense. The documentation is also a little strange :
It is the implementation signature that is the catch-all case, not the last call signature. |
The documentation refers to call signatures. You can't call the implementation signature. But I guess the wording could be clearer.
That depends on your code. In your example there is no "catch-all" call signature. But you could add one: function foo(a: boolean): void
function foo(a: number): void
function foo(a: boolean | number): void
function foo(a: boolean|number) {} Now you have a catch-all call signature, and your function could be called with a value typed |
I think I see 3 issues for this. The first one is that the third signature would be callable, so would enable to call e.g. The second one is the code duplication between the last call signature and the implementation signature (code duplication is never a great thing). The third one is that if we wish to use it on existing functions, we'd have to declare it for every functions which can be really time-consuming, with the risk of forgetting one, or if one function is modified, to forget to update the declaration. Knowing that, without overload, TS already generates a signature from the implementation signature. For example, if we assume that TS generates a list of signatures, could we assume that e.g. the first one is non-callable (if other signatures are available), and reserve it for the implementation signature and inferences ? If empty (e.g. no implementation signature known), when doing inferences, could either use the last signature from the list (current behavior), or could even be computed as the union of the other signatures ? Technically I'd assume this would be possible ? |
So is this now some kind of feature request? TS is behaving as designed and weβre talking aboutβ¦ implementation signature changes? Whatβs the status of this as a bug report? |
Currently it seems the issue is a design issue. I think you consider them to be feature requests ? |
I really don't know, and I'm not a member of the TS team, just a My guess is that they would just decline a suggestion to expose the implementation signature the way you're asking for. Function types don't encode implementation information, they encode call signature information. Declarations for overloads don't mention the implementation, and they shouldn't have to know anything about the implementation (or else TS's library files for JS would need to have all kinds of added information in them). And what good would it do to consult the implementation signature when the function is not callable that way? If you tell me Anyway I think I've said all I can say here so I will bow out and wait to hear what the TS team says. Good luck! |
Thanks for your answer.
I agree. I assumed it was not possible as the doc stated:
And hard to fix, as the mentioned issues are 5 to 7 years old, without being addressed, while it the issue seems quite real (TS seems to have a lot of such issues, it is kind of frustrating). Indeed, as you stated :
You are quite right. It is even more true if we have 2 arguments : However, I saw that the type has the different callable signatures :
So TS seems to have all required information to perform a correct inference. But I think I got the issue. Possible solution 1: For a simple inference, we could simply try it for each callable signature, and return an union of the inferred types. type Args<T> = T extends (a: infer A, b: infer B) => infer C ? [A,B, C] : never; Could give Possible solution 2: Else, the union could be made latter, by making an union of the results, e.g. returning : Possible solution 3: Adding to TS the types from the solution given inside the issue you just posted: |
I don't see anything here that isn't addressed in other issues.
Every issue you encounter leads to an open issue rather than a closed one, interesting observation π |
The interesting observation is that lot of theses issues are rather old than new, with multiples instances, and often due to design choices... some issues never got any clear answer whether they'll be fixed one day, its priority level, what prevent/delay a fix, etc. I have so much fun wasting time on "unsound" stuff, and trying to find dirty workarounds if any. |
π Search Terms
None
π Version & Regression Information
Tested on the nightly build.
β― Playground Link
tsx function foo(a: boolean): void function foo(a: number): void function foo(a: boolean|number) {} type InferArgs<T> = T extends (...args: infer U) => void ? U : never; type Args = InferArgs<typeof foo...
Playground Linkπ» Code
π Actual behavior
When inferring the function parameters, only one of the function signature is used.
Here,
Args
is[number]
, which raises an error in the last line.π Expected behavior
When inferring the function parameters, the union of the different signatures should be returned.
Here,
Args
should be[number]|[boolean]
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: