-
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
Type inference for rest parameters differs from that for arrays #41048
Comments
These are different syntaxes and thus mean different things; by having different syntax do different things you are then able to express different behavior depending on which you want to happen. Changing either one of these behaviors would be a fairly large breaking change and would need to be justified by something more than "it's different". |
I don't think it works as intended: the |
This is the intended behavior. In |
I can't agree with you. MDN states: "A function's last parameter can be prefixed with ... which will cause all remaining (user supplied) arguments to be placed within a "standard" JavaScript array." But this is not the case with TS: for rest parameters it fails to deduce the same union type as it does for array. I'd expect it to infer |
And in any case, all four examples are valid, if TS can't (or does not want to) infer the correct union type, it should have set |
"valid" and "correct" are outcomes decided by the type system, and the type system has intentionally been designed with flagging some of these as errors as not others. |
The type system is wrong then: semantically rest parameters do not differ from arrays is JS, while TS assign them different behaviour. The |
TypeScript Version: 4.1.0-dev.20201011
Search Terms: rest parameters, type inference
Expected behavior: All test cases are valid ones.
Actual behavior: cases
t3
andt4
are correctly understood by the TS compiler, the other ones result in errors:Related Issues: #37193
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: