-
Notifications
You must be signed in to change notification settings - Fork 12.9k
A parameter not declared as a rest parameter is not one #18825
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
ES3/5 syntax does not allow for dot-dot-dot syntax. Is there any way to declare rest parameter signature when using TS with ES3/5 JavaScript? |
@mihailik I don't think there is, but we'll give you one for free anyway. function f() {
return arguments[1];
}
f(1, 2); // Works, because `arguments` appears in the function body. |
@Andy-MS that looks like best of both worlds, neat! However I've just checked with
/** @param {...*} a */
function f(a) {
return a;
}
f(1, 2).push(3); Compiles with no error. Either I misunderstood this PR, or something is not right? To be specific: |
Thanks, I got the intended checking — no error on 2.5.2, error on typescript@next. But the magic
/** @param {...*} a */
function f(a) {
return [arguments[arguments.length-1]];
}
f(1, 2).push(3);
~
multi.js(5,3): error TS2345: Argument of type '1' is not assignable to parameter of type 'any[]'. To be specific: |
That's because it's listening to the jsdoc which declares that |
@Andy-MS your original point seems to be correct (same according to Stackoverflow referring to JSDocs spec and Google Closure): We think a is a rest parameter because the JSDoc declares it that way Which is opposite to what you're saying right here above: That's because it's listening to the jsdoc which declares that a is an array My concern is to make sure idiomatic ES5-style code exposing API with variable parameters is handled the right way. Thanks a lot! |
Well, in the first example I'm describing a bug! If |
@Andy-MS looks like we have another bug introduced here. Here's a bit clearer example from above:
/** @param {...number} a */
function f(a) {
console.log(arguments[0]);
}
f([1, 2]); // this should fail, but passes
f(1, 2); // this should pass, but fails
~
multi.js(6,3): error TS2345: Argument of type '1' is not assignable to parameter of type 'number[]'. This is a valid idiomatic proper JavaScript: declare a function with variable number arguments. Compiler incorrectly assumes the argument is of type |
Well, like I said above, we could add a diagnostic for if you declare a rest parameter in JSDoc but the corresponding parameter ( |
Then it's a regression, unfortunately. That is a valid way to implement rest parameter in ES5. |
Was looking at this with @sandersn yesterday.
Currently we allow this code:
We think
a
is a rest parameter because the JSDoc declares it that way, but that doesn't change the fact that it isn't really a rest parameter.