-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Error on excess spread arguments #20071
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
Conversation
Make the *technically* correct construction illegal: ```ts declare function f(n: number): void; declare var ns: number[]; f(1, ...ns); ``` This call only makes sense if `ns = []`, but in that case, why pass `ns` at all? Allowing this call masks other errors when functions are refactored to have fewer parameters, or to stop using rest parameters: ```ts declare function old(...ns: number[]): void; declare function new(ns: number | number[]): void; old(1, ...ns); // Fine! new(1, ...ns); // Should error! ``` This change the error for excess spread arguments to be more understandable: "Expected 3 arguments, but got least 4". Previously the error would have been "Expected 3 argument, but got at least 3", which is, again, technically correct, but not understandable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gnerally good, just a small comment.
normal("g", ...ns) | ||
normal("h", ...mixed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these mixed
and tuple
cases not still be somewhere? (If only to assert they behave similarly)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, correctness is determined syntactically, so the types of the spread arguments don't matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright. 👍
thunk(...mixed) | ||
thunk(...tuple) | ||
~~~~~~~~~~~~ | ||
!!! error TS2556: Expected 0 arguments, but got a minimum of 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be pedantic, but then again so is this error: I think it'd be more clear to say Expected N arguments, but got Y or more.
when a spread is involved (where N
is the maximum arity, and Y
is the number of non-spread arguments).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, good idea. I'll see how that looks if I change all of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is much easier to read. Nice suggestion. (I feel like it's a tiiny bit less accurate, but that doesn't matter much.)
Thanks @weswigham for the improved wording.
Fixes #19900
Make the following call with spread illegal:
This call, while technically legal, only makes sense if
ns = []
, but in that case, why passns
at all? Allowing this call masks other errors when functions are refactored to have fewer parameters, or to stop using rest parameters:This PR also changes the error for excess spread arguments to be more understandable:
"Expected 3 arguments, but got least 4".
Previously the error would have been "Expected 3 argument, but got at least 3", which is, again, technically correct, but not understandable.