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

Type 'any[]' is not assignable to type '[any, any]' when constructing a Map from array of entries #22870

Closed
mohsen1 opened this issue Mar 25, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Mar 25, 2018

During my work at webpack/webpack#6862 I noticed this error.

TypeScript Version: 2.7.8-rc

Search Terms:

Code
https://github.com/webpack/webpack/blob/master/lib/util/objectToMap.js

function objectToMap(obj) {
	return new Map(Object.keys(obj).map(key => [key, obj[key]]));
};

Expected behavior:
No error

Actual behavior:


lib/util/objectToMap.js:9:17 - error TS2345: Argument of type 'any[][]' is not assignable to parameter of type 'Iterable<[any, any]>'.
  Types of property '[Symbol.iterator]' are incompatible.
    Type '() => IterableIterator<any[]>' is not assignable to type '() => Iterator<[any, any]>'.
      Type 'IterableIterator<any[]>' is not assignable to type 'Iterator<[any, any]>'.
        Types of property 'next' are incompatible.
          Type '{ (value?: any): IteratorResult<any[]>; (value?: any): IteratorResult<any[]>; }' is not assignable to type '{ (value?: any): IteratorResult<[any, any]>; (value?: any): IteratorResult<[any, any]>; }'.
            Type 'IteratorResult<any[]>' is not assignable to type 'IteratorResult<[any, any]>'.
              Type 'any[]' is not assignable to type '[any, any]'.
                Property '0' is missing in type 'any[]'.

9  return new Map(Object.keys(obj).map(key => [key, obj[key]]));
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 26, 2018

I think this looks like a duplicate of #11152.

@MartinJohns
Copy link
Contributor

The tuple is inferred to be an array. As a temporary workaround you can just specify the exact type when calling map:

array.map<[Type1, Type2]>(key => [key, obj[key]])

That will make it explicit that you want a tuple.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 26, 2018

in a .js file you can cast using /** @type ... */:

function objectToMap(obj) {
	return new Map(Object.keys(obj).map(key => /** @type [string, string] */ ([key, obj[key]]));
};  

@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 26, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants