Skip to content
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

Array pattern in destructuring assignment do not typecheck correctly when assigned to an object. #733

Closed
eyyub opened this issue Aug 20, 2015 · 7 comments

Comments

@eyyub
Copy link
Contributor

eyyub commented Aug 20, 2015

/* @flow */

var [a, b] = {foo:3, b:'lol'}; // no error
(a : string);
(a : number);
(a : void);
(a : any);
(b : void);
(b : any);

var {p} = [1]; // error, ok
var [a, b] : [number | string] = {foo:3, b:'lol'}; // error due to type annotation, ok

It seems it comes from here.
Maybe in destructuring we should ensure that the type are "compatible"(good english word ?).

@kkirby
Copy link
Contributor

kkirby commented Aug 21, 2015

I'm not sure that this is really a bug. Destructing works on objects that are iterators. For example, this is valid JavaScript:

let [a,b] = {
  0:'A',
  1:'B',
  [Symbol.iterator]: function*(){
    yield this['0'];
    yield this['1'];
  }
};
console.log(a,b); // Outputs "A B"

But in your later example, since an object cannot be an array, an error is thrown.

@eyyub
Copy link
Contributor Author

eyyub commented Aug 21, 2015

I see, at least it should be handled as a special case(actually Flow doesn't support this construct) , but IMHO var [a, b] = {foo:3, b:'lol'} should not be allowed.

@samwgoldman
Copy link
Member

Yeah, we should be able to constrain the RHS to be an iterable in array destructuring assignments.

@TrySound
Copy link
Contributor

TrySound commented Feb 9, 2019

@samwgoldman This became a more relevant problem since hooks were introduced. I convert tuple api to object and miss some places.

@kevinbarabash
Copy link

I ran into this today. The current behavior results in variables that are typed as any. I should probably code with type coverage always enabled as that would've detected the issue with my code.

@somewhatabstract
Copy link

See also #2612

@gkz
Copy link
Member

gkz commented Dec 11, 2023

Fixed in 80ca16f

@gkz gkz closed this as completed Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants