0

Accordint to MDN doc

initializing a Map from an array like this is legitimate:

let kvArray = [['key1', 'value1'], ['key2', 'value2']];
let myMap: Map<string, string> = new Map(kvArray);

Despite, my Visual Studio Code compiler complains with this:

No overload matches this call.
Overload 1 of 3, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
Argument of type 'string[][]' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.

1 Answer 1

1

The problem is that you havn't put a type on kvArray, so typescript is inferring it. It infers that it's a string[][], but that's not specific enough. The Map constructor needs tuples with exactly two strings, not arrays with an arbitrary number of strings. Your javascript is creating these tuples, but you'll need to be explicit about the types:

let kvArray: [string, string][] = [['key1', 'value1'], ['key2', 'value2']];
Sign up to request clarification or add additional context in comments.

1 Comment

That, indeed, makes the compiler happy. Thanks. (Strange declaration syntax, though).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.