-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Excess spread arguments are not errored #19900
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
Comments
When I tried to fix this, I found the test that explains why it works this way. It's for functions that use function mixed(fixed: number) {
Debug.assert(fixed > 0);
for (var i = fixed; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
mixed(2, 'hello', 'world', 'goodbye', 'goodbye') // ok ... I guess? I guess this makes sense, but I think the right thing to do is only allow calls like this for functions that use |
is that in a .js file? |
The test is a .ts file; I haven't tracked the comment in the test back to a discussion on github yet. Maybe restricting that usage to javascript would be an acceptable solution too. |
The call is an error in TS if you do not have the spread. i.e. function mixed(fixed: number, ...args: string[]) {
Debug.assert(fixed > 0);
for (var i = fixed; i < arguments.length; i++) {
console.log(arguments[i]);
}
} or function mixed(fixed: number, ...args: string[]);
function mixed(fixed: number) {
Debug.assert(fixed > 0);
for (var i = fixed; i < arguments.length; i++) {
console.log(arguments[i]);
}
} |
Ah, I see. Yes, the argument has to be a spread, which is what @kitsonk actually has in his code base. I mistakenly converted it to individual arguments. Here's the actual test case from cases/conformance/expressions/functionCalls/callWithSpread2.ts: declare function normal(s: string): void;
declare numbers: number[];
normal("i", ...numbers); I think this should be disallowed. The question is whether it should be allowed if this is the definition of function normal(s: string) {
console.log(s);
console.log(arguments[1]);
} |
That looks wrong.. i would be interested to see if any RWC/user code test fails because of that. |
function normal(s: string) {
console.log(s);
console.log(arguments[1]);
} That is exactly the sort of JS bs TypeScript should help us fix as well... I suspect it will break some user code because if you are porting some code over, and TypeScript didn't force you to change it, you certainly wouldn't fix it... I guess it is arguable if it should be strict or not, since it would be a breaking change, but it seems like forcing someone to add an argument (or a spread argument) to strongly type their arguments is worth it for the sanity of code. |
Nope, no failures from RWC or user code. |
Here's a counter-example that I thought about and then decided should still be illegal: function f() { return arguments[0] }
declare var ns: number[];
f(...ns) By analogy with what we allow in Javascript: function j() { return arguments[0] }
j(1,2,3) However, if you're using Typescript, or a version of Javascript that supports |
Fix is up at #20071 |
TypeScript Version: 2.6.1
Code
It appears that extra arguments that are spread arguments are always accepted, which can lead to unintentional errors in the code. For example the following:
This is related to #4130 but @sandersn suggested a separate issue, as this subset of the larger issue is likely addressable, where as determining if a spread argument might result in longer arguments is difficult, versus cases where it is easily statically identifiable (this case).
Expected behavior:
That an extra spread argument errors.
Actual behavior:
An excess spread argument does not error.
The text was updated successfully, but these errors were encountered: