Closed
Labels
ES6Relates to the ES6 SpecRelates to the ES6 SpecIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
When trying to pass caller arguments to another function with ...arguments the compiler complains with Type 'IArguments' is not an array type. Version 1.8.9.
Reacted by AA, Aaron Resnick, Code Scratcher, Emil Johansson, Cory Kleiser and Jan Molak
Metadata
Metadata
Assignees
Labels
ES6Relates to the ES6 SpecRelates to the ES6 SpecIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
jwbay commentedon Mar 20, 2016
The arguments object is not an array type (though it resembles one), so the compiler error seems correct. Accept the arguments with a rest operator and they'll get converted to an array.
iby commentedon Mar 20, 2016
Now when you say it, I remember looking it up a dozen times now… Still, that functionality would be natural, especially given that
applycan be used directly witharguments.iby commentedon Mar 28, 2016
Mohamed Hegazy (@mhegazy) I wasn't asking a question. This works in javascript, typescript fails to compile.
argumentsis not an array, it's a fact, but it should be compatible with the spread operator.mhegazy commentedon Mar 28, 2016
it is not an array. quoting MDN:
it goes on to say:
so,
iby commentedon Mar 29, 2016
We're having a miscommunication. I am not saying it's an array, I'm stating the opposite, that it is not array, but it should be compatible with the spread operator.
This throws a compile error, but produces a working code. Naturally
argumentswould be expected to work with the spread operator.mhegazy commentedon Mar 29, 2016
i see.
well. some of the transformation on a spread uses
slice. which does not exist. in theory we can allow ArrayLike, and callArray.prototype.slice.apply(v)instead ofv.slicemhegazy commentedon Mar 29, 2016
updating the title.
[-]Type 'IArguments' is not an array type[/-][+]Allow ArayLike as a spread soruce[/+][-]Allow ArayLike as a spread soruce[/-][+]Allow ArayLike as a spread source[/+]kitsonk commentedon Mar 30, 2016
It isn't valid ES6 syntax:
It fails in Firefox (but not in Chrome or Edge) and so would have to be transformed even when targeting ES6 with little or no value. It was the "magic" behaviours of
argumentswas a big part of the value of the spread/rest operator in the first place.17 remaining items
mhegazy commentedon May 11, 2016
oh. did not think of that. duh.. back to the drawing board then :)
pablobirukov commentedon Jan 3, 2017
[...document.querySelectorAll(".foo")]triggersType 'NodeListOf<Element>' is not an array type(with target: "es5").But I think it would be nice to have it successfully transpiled to
Array.prototype.slice.call(document.querySelectorAll(...))sinceNodeListOf<Element>is iterablemhegazy commentedon Jan 3, 2017
This should be addressed by #12346
aaron-resnick commentedon Dec 29, 2017
I believe #12346 did not address this, as I'm still seeing the same error in TS 2.6.2
kitsonk commentedon Dec 29, 2017
aaronresnick as mentioned in the PR text, when targeting ES3/ES5 you have to use
--downlevelIteration. Are you using that?vitaly-t commentedon Feb 26, 2018
So, to clarify, as of this day it still doesn't work, and the reason being - it doesn't work in FireFox.
In the meantime, all our Node.js code in JavaScript uses
argumentsfor spread operator without any issues, but the TypeScript can't/won't do it regardless, for compatibility with FireFox.Shouldn't support for spread of array-like objects become the default at this point, or at least default when building for the server-side?
[-]Allow ArayLike as a spread source[/-][+]Allow ArrayLike as a spread source[/+]fregante commentedon May 24, 2019
The spread operator only works on
Iterables andArrayLikes aren'tIterables.Try this anywhere:
argumentshowever is bothIterableandArrayLike, and it already works correctly in TypeScript:This issue should be closed as resolved or invalid.
sushruth commentedon Mar 12, 2021
Valid -
I have now realized that
ReadonlyArray<>is a better tool for this job thanArrayLike<>. Text below is invalid.Invalid -
One problem I had that led me to this issue was not being able to type spread arguments as
ArrayLikelike so -However there is a tiny workaround for this -
I hope this helps some.
RyanCavanaugh commentedon Jan 25, 2024
This works now