Skip to content

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

Merged
merged 3 commits into from
Nov 16, 2017
Merged

Conversation

sandersn
Copy link
Member

Fixes #19900

Make the following call with spread illegal:

declare function f(n: number): void;
declare var ns: number[];
f(1, ...ns);

This call, while technically legal, 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:

declare function old(...ns: number[]): void;
declare function nya(ns: number | number[]): void;
old(1, ...ns); // Fine!
nya(1, ...ns); // Should error!

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.

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.
Copy link
Member

@weswigham weswigham left a 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)
Copy link
Member

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)

Copy link
Member Author

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.

Copy link
Member

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.
Copy link
Member

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).

Copy link
Member Author

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.

Copy link
Member Author

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.)

@sandersn sandersn merged commit 2263a1e into master Nov 16, 2017
@sandersn sandersn deleted the error-on-excess-spread-arguments branch November 16, 2017 19:00
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants