GeezerVison is showing github.com — Allow ArrayLike as a spread source · Issue #7596 · microsoft/TypeScript
Allow ArrayLike as a spread source #7596DescriptionWhen 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 Activityjwbay commented on 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.
function foo(...args) {
bar(...args);
}Reacted by Ian Bytchek, jeffreymorlan and Code Scratcher iby commented on 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 apply can be used directly with arguments. iby commented on Mar 28, 2016 Mohamed Hegazy (@mhegazy) I wasn't asking a question. This works in javascript, typescript fails to compile. arguments is not an array, it's a fact, but it should be compatible with the spread operator. Reacted by Code Scratcher and Cory Kleiser mhegazy commented on Mar 28, 2016 it is not an array. quoting MDN:
The arguments object is an Array-like object corresponding to the arguments passed to a function.
it goes on to say:
The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array
so,
function f() {
arguments.push(0); // Runtime error: Uncaught TypeError: arguments.push is not a function(…)
arguments.indexOf(0); // Runtime error: Uncaught TypeError: arguments.indexOf is not a
Array.isArray(arguments); // false
}
f(1,2);iby commented on 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.
function foo(){
console.log(...arguments);
}
foo('bar', 'baz');
This throws a compile error, but produces a working code. Naturally arguments would be expected to work with the spread operator. mhegazy commented on 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 call Array.prototype.slice.apply(v) instead of v.slice Reacted by Ian Bytchek, Richard Simpson, Pavel Birukov and Jan Molak mhegazy commented on Mar 29, 2016 changed the title [-]Type 'IArguments' is not an array type[/-] [+]Allow ArayLike as a spread soruce[/+] on Mar 29, 2016changed the title [-]Allow ArayLike as a spread soruce[/-] [+]Allow ArayLike as a spread source[/+] on Mar 29, 2016kitsonk commented on Mar 30, 2016 It isn't valid ES6 syntax:
'use strict';
function bar() {
console.log(arguments);
}
function foo() {
bar.call(this, ...arguments);
}
foo('bar', 'qat');
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 arguments was a big part of the value of the spread/rest operator in the first place. mhegazy commented on May 11, 2016 oh. did not think of that. duh.. back to the drawing board then :) removed this from the This milestone has been deleted milestone on May 11, 2016pablobirukov commented on Jan 3, 2017 [...document.querySelectorAll(".foo")] triggers Type '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(...)) since NodeListOf<Element> is iterable
mhegazy commented on Jan 3, 2017 This should be addressed by #12346 aaron-resnick commented on Dec 29, 2017 I believe #12346 did not address this, as I'm still seeing the same error in TS 2.6.2 kitsonk commented on Dec 29, 2017 aaronresnick as mentioned in the PR text, when targeting ES3/ES5 you have to use --downlevelIteration. Are you using that? Reacted by Cory Kleiser and ueli k Reacted by Cory Kleiser vitaly-t commented on 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 arguments for 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? Reacted by Cory Kleiser, Mike Murkovic and Sushruth Sastry changed the title [-]Allow ArayLike as a spread source[/-] [+]Allow ArrayLike as a spread source[/+] on Jun 18, 2018fregante commented on May 24, 2019 The spread operator only works on Iterables and ArrayLikes aren't Iterables.
// from lib.es5.d.ts
interface ArrayLike<T> {
readonly length: number;
readonly [n: number]: T;
}
Try this anywhere:
[...{length: 0}];
// TypeError: ({length:0}) is not iterable
arguments however is both Iterable and ArrayLike, and it already works correctly in TypeScript:
This issue should be closed as resolved or invalid. sushruth commented on Mar 12, 2021 Valid -
I have now realized that ReadonlyArray<> is a better tool for this job than ArrayLike<>. Text below is invalid.
Invalid -
One problem I had that led me to this issue was not being able to type spread arguments as ArrayLike like so -
function getProps<K extends ArrayLike<keyof User>>(...props: K) {
// above line gives this error at `...props: K` - A rest parameter must be of an array type.(2370)
}
However there is a tiny workaround for this -
function getProps2<K extends ArrayLike<keyof User>>(...props: K[number][]) {
// No error above!
}
I hope this helps some. RyanCavanaugh commented on Jan 25, 2024
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|