-
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
tuple inference in array vs rest #34865
Comments
I don't know why this happens but my personal experience aligns with yours. TS infers more stuff, and is more accurate with inferences when it comes to tuple rest parameters. It's why I tend to avoid "regular" tuple parameters nowadays. Also, 3.6.3 TS Playground |
This is basically a design limitation for the same reason that this wouldn't work with an explicit parameter list if the parameters were in the opposite order - given multiple context-sensitive things that need inference, we proceed in left-to-right order of parameters, including for expansion of rest tuples. When the inference sites are all part of a single argument tuple, there's no mechanism or heuristic to do multiple rounds of inference - it's the same as if we were trying to infer to multiple properties of the same object (in fact, it's exactly that, since a tuple in a non-rest position is just an object as far as inference is concerned). See #30134 for general discussion of an algorithm that would be able to handle this. |
Is it because the function with rest params is converted to a function without rest params first? So, |
@RyanCavanaugh is there any sort of workaround I can employ? I can't use the rest parameter approach as my actual code looks more like this: |
@AnyhowStep correct @jackmellis I can't think of a workaround that doesn't have effects on how the function would be called. TS just can't do multiple rounds of inference against the same object, so the inference sites need to be split up across either multiple parameters or multiple calls |
TypeScript Version: typescript@3.8.0-dev.20191101
Search Terms: generic, tuple, rest
Code
Expected behavior:
type is:
Actual behavior:
Parameter
c
isunknown
In v3.3.x which I was working with (before testing against latest)
c
was inferred as{}
, so the return type would be{ height: 100 }
(i.e. all other properties were dropped).If you do the exact same code with a rest parameter instead of an array, it works absolutely fine:
Playground Link: https://codesandbox.io/embed/typescript-playground-export-kbre9
The text was updated successfully, but these errors were encountered: