Skip to content

ES6 destructuring with this variable - tuple and array #10076

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

Closed
Guardiannw opened this issue Aug 1, 2016 · 6 comments
Closed

ES6 destructuring with this variable - tuple and array #10076

Guardiannw opened this issue Aug 1, 2016 · 6 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Guardiannw
Copy link

Guardiannw commented Aug 1, 2016

TypeScript Version: 1.8.10
Code

// A *self-contained* demonstration of the problem follows...
var array = [1, 2, 3];
[this.test, this.test2, this.test3] = array;

Expected behavior:
for it to compile as _this.test = array[0], _this.test2 = array[1], _this.test3 = array[2];
Actual behavior:
it compiles as _this.test = array[0], this.test2 = array[1], this.test3 = array[2];

This is a problem because the this scope does not match correctly that of the lambda.

@RyanCavanaugh
Copy link
Member

Can't repro on the Playground this with the code given. Can you post a complete sample (with the surrounding function) ?

@Guardiannw
Copy link
Author

Guardiannw commented Aug 1, 2016

@RyanCavanaugh I could not reproduce this on the playground either. But strangely i figured out a fix.

When I would write something to the effect of

wrapper((array: [any]) => {
    [this.test, this.test1, this.test2] = array;
});

it would fail and would output as I had previously stated.
But when I changed the type, it worked correctly ->

wrapper((array: Array<any>) => {
    [this.test, this.test1, this.test2] = array;
});

I am not sure why this is. I thought the type did not change transpilation, but only is there for transpile time type checking, but it looks like it matters for that too.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 2, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.0.1 milestone Aug 2, 2016
@RyanCavanaugh
Copy link
Member

Definitely a bug

declare function wrapper(x: any);
wrapper((array: [any]) => {
    [this.test, this.test1, this.test2] = array;
});

should output three _thiss

@yuit
Copy link
Contributor

yuit commented Aug 8, 2016

@Guardiannw the reason why
this example

wrapper((array: [any]) => {
    [this.test, this.test1, this.test2] = array;
});

and this one

wrapper((array: Array<any>) => {
    [this.test, this.test1, this.test2] = array;
});

behaves differently is that the first example array has type tuple with one element (that is type any); when compiler type-check destructing assignment for the second element in tuple, it couldn't get type for that element so this expression isn't properly check (hence the flag to determine whether to capture lexically this doesn't get check as well). The second example is just an array so it doesn't affect by the similar issue

@Guardiannw
Copy link
Author

@yuit That makes complete sense, but I thought that the types did not create any changes in the exported code, that they only made compile time type checks. Is this correct or incorrect with the desired specs?

@yuit
Copy link
Contributor

yuit commented Aug 9, 2016

@Guardiannw I think this is incorrect and undesirable that we skip checking this expression and doesn't set the flag properly. Emitted code of both cases should be the same although one will give you a compiler error.

I have merged the fix into release-2.0 and master if you would like to give it a try!

@Guardiannw Guardiannw changed the title ES6 destructuring with this variable ES6 destructuring with this variable - tuple and array Aug 9, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 1, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants