TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Destructuring the arguments variable

Open tylergoodman opened this issue 11 years ago • 23 comments

Doesn't work because the IArguments interface isn't defined as an Array type. Is there a reason behind this?

Example:

function test () {
  var [a] = arguments; // Error: Type 'IArguments' is not an array type.
}

One solution is doing this:

function (...args) {
  var [ one, two, three ] = args;
}

tylergoodman avatar May 22 '15 08:05 tylergoodman

Because arguments is not an array. It is "Array-like" but it isn't a true array.

kitsonk avatar May 22 '15 13:05 kitsonk

var [a] = Array.from(arguments) should work here.

saschanaz avatar May 22 '15 16:05 saschanaz

As a side question, why don't you use parameters instead of destructuring the arguments object? Is there a specific use-case you're trying to accomplish?

DanielRosenwasser avatar May 22 '15 17:05 DanielRosenwasser

var [a] = Array.from(arguments) should work here.

Array.from is only ES6 and only implemented in Firefox actually. var [a] = Array.prototype.slice.call(arguments) is better for now.

kitsonk avatar May 23 '15 05:05 kitsonk

@kitsonk I understand that the arguments variable is an array-like. But aren't array-destructuring assignments of array-like objects valid javascript?

ES6 will only allow array-destructuring of an array-like if the object has an iterator. Here's an example of what I mean:

var obj = {
  0: 'foo',
  1: 'bar',
  2: 'baz',
  length: 3,

  // required
  * [Symbol.iterator]() {
    for (var i = 0, len = this.length; i < len; i++) {
      yield this[i];
    }
  }
};

var [a, b, c] = obj;
console.log(a, b, c); // 'foo bar baz'

Because arguments is supposed to be iterable in ES6, this should work too:

function fun () {
  var [a, b, c] = arguments;
  console.log(a, b, c); // 'foo bar baz'
}

fun('foo', 'bar', 'baz');

I don't think anyone has this implemented arguments being iterable yet though as far as I know.


In Typescript, destructuring assignments made while in ES6 mode compile directly to ES6 destructuring assignments

// this javascript and typescript are equivalent
var arr = ['foo', 'bar', 'baz'];
var [a, b, c] = arr;

And in ES3/5 mode the assignment is converted into a series of assignments

var arr = ['foo', 'bar', 'baz'];
var a = arr[0], b = arr[1], c = arr[2];

In both modes, the resulting javascript works (or will work (right?)). Is there a reason that array-destructuring of arguments and other array-like objects isn't allowed? Is it just something that hasn't been fully looked at yet?

tylergoodman avatar May 24 '15 22:05 tylergoodman

I understand that the arguments variable is an array-like. But aren't array-destructuring assignments of array-like objects valid javascript?

But you are using TypeScript and using an implicit cast which doesn't work, ergo the Error: Type 'IArguments' is not an array type.

In Typescript, destructuring assignments made while in ES6 mode compile directly to ES6 destructuring assignments

This is assigning an Array to an Array. Not an array-like to an Array. Destructing assignments !== breaking typing rules in TypeScript.

kitsonk avatar May 25 '15 06:05 kitsonk

@kitsonk

But you are using TypeScript and using an implicit cast which doesn't work, ergo the Error: Type 'IArguments' is not an array type.

I haven't checked the ES2015 spec, but if it says that the destructuring syntax can be applied to an array-like object with an iterator, then I think it should be valid TS. No implicit cast to array should be required here and that's the bug in the first place.

It is my understanding that TS is designed to be a superset of JS and it fails here. Not that I think this feature is mega-useful but just sayin'.

jods4 avatar May 27 '15 22:05 jods4

Chrome supports arguments[Symbol.iterator] but Firefox does not, so the code will cause an error on Firefox. Is this in the ES6 spec?

saschanaz avatar May 28 '15 05:05 saschanaz

Is this in the ES6 spec?

From my understanding, yes. See the CreateUnmappedArgumentsObject and CreateMappedArgumentsObject abstract operations.

Given the direction that we've been taking with arguments recently, I'm inclined to liken this to "doctor it hurts when I use the arguments object".

There's already a fairly sufficient way of destructuring arguments: formal parameters.

Still, as a matter of consistency, I'm going to ping @JsonFreeman because of he's done some recent work on destructuring that might need to be informed regarding this.

DanielRosenwasser avatar May 28 '15 07:05 DanielRosenwasser

@DanielRosenwasser I agree 100% that arguments have no good use in ES6. But note that the real issue here is more general. It's not just arguments that doesn't work but any array-like, iterable object.

I have no good use-case and don't really care (right now), but this is a deviation from ES6 spec and it could be that some people find clever use for iterable objects. After all, this is still new and mostly unused stuff.

So the question is really: how compatible with ES2015 does TS want to be?

jods4 avatar May 28 '15 09:05 jods4

NodeList can be another example, while I don't know this is in spec or not. NodeList objects have iterator in Firefox but not in Chrome.

saschanaz avatar May 28 '15 10:05 saschanaz

Does arguments have slice? In order to destructure with a rest element, you need slice.

JsonFreeman avatar May 28 '15 17:05 JsonFreeman

@JsonFreeman in my mind, we'd permit destructuring arguments when targeting ES6 (because IArguments should have an iterator). Otherwise, don't bother.

DanielRosenwasser avatar May 28 '15 18:05 DanielRosenwasser

Or use Array.prototype.slice.call(arguments).

saschanaz avatar May 28 '15 18:05 saschanaz

I guess we could support it in ES6 if there is enough demand.

JsonFreeman avatar May 28 '15 18:05 JsonFreeman

Funny coincidence. Yesterday Mozilla has published the new part in their ES6 In Depth series, this time about destructuring: https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/

This has quite some visibility and it talks about destructuring iterables. There is one chapter called Destructuring arrays and iterables and it ends with an explicit mention of array assignment pattern working for any iterable plus a code example (your classical fibonacci sequence).

Note that destructuring assignment with an array assignment pattern also works for any iterable:

function* fibs() {
  var a = 0;
  var b = 1;
  while (true) {
    yield a;
    [a, b] = [b, a + b];
  }
}
var [first, second, third, fourth, fifth, sixth] = fibs();
console.log(sixth);
// 5

jods4 avatar May 29 '15 10:05 jods4

This already works in typescript. The type system allows you to destructure an iterable into an array. The problem is that IArguments is not iterable in lib.d.ts. The fix is to make it iterable.

JsonFreeman avatar May 29 '15 17:05 JsonFreeman

I have a PR (#3306) so that you'll be able to do this when using ES6 as a target.

DanielRosenwasser avatar May 29 '15 22:05 DanielRosenwasser

The change is now in master.

DanielRosenwasser avatar May 29 '15 23:05 DanielRosenwasser

Thanks for the fix @DanielRosenwasser . Concerning ES5-targeted code, is there any chance that array-destructuring of array-like objects will be implemented in the future? TS would have to detect that it's an object and call the .slice() function on the prototype instead of calling it directly on the variable, I guess. Something like this.

// typescript
var arrayLike = {
    '0': 'foo',
    '1': 'bar',
    '2': 'baz'
};
var [foo, ...bar] = arrayLike;
console.log(foo, bar); // 'foo', ['bar', 'baz']
// output
var arrayLike = {
    '0': 'foo',
    '1': 'bar',
    '2': 'baz'
};
var foo = arrayLike[0], bar = arrayLike.slice(1); // error
// desired output
var arrayLike = {
    '0': 'foo',
    '1': 'bar',
    '2': 'baz'
};
var foo = arrayLike[0], bar = Array.prototype.slice.call(arrayLike, 1);
console.log(foo, bar); // 'foo', ['bar', 'baz']

tylergoodman avatar Jun 03 '15 04:06 tylergoodman