I would like to use a (K | V)[][]
as ReadonlyArray<[K, V]>
in a Map constructor. Lets say that our V is an interface IItem
and K a basic number
.
interface IItem {
key: number;
val: string;
}
const items = [[1, {key: 1, val: "a"}]]
const ze_map = new Map<number, IItem>(items);
console.log(JSON.stringify([...ze_map]));
By default, typescript will perceive the type of items as : (number | IItem)[][]
. This will not work and throw an error:
Argument of type '(number | { key: number; val: string; })[][]' is not assignable to parameter of type 'ReadonlyArray<[{}, {}]>'.
Type '(number | { key: number; val: string; })[]' is missing the following properties from type '[{}, {}]': 0, 1
Luckily for us we can force the type to Array<[number, IItem]>
to please Map
.
const items2 : Array<[number, IItem]> = [[1, {key: 1, val: "a"}]]
const ze_map2 = new Map<number, IItem>(items2);
console.log(JSON.stringify([...ze_map]));
This work as expected. Alright, lets move to my problem. What if we cannot force the type?
const arr = [
{ key: 3, val: 'bar' },
{ key: 5, val: 'world' }
];
const result: Array<[number, IItem]> = arr.map(i => ([i.key, i]));
const ze_map3 = new Map(result);
console.log(JSON.stringify([...ze_map]));
This will not work because here (number | { key: number; val: string; })[][]
is not Array<[number, IItem]>
. So, how to use (K | V)[][]
as ReadonlyArray<[K, V]>
in a Map?
You can try all the code online
I read both How to define Map with correlation between a key type and a value type, while they both are unions and Typescript Map<enum, set<enum>> "No overload matches this call", but I don't get why? without finding the solution. I may missed the solution.
I also read the MDN entry on Map which say that it should work as expected and if you try in vanilla JS it does work as expected:
var arr = [
{ key: 3, val: 'bar' },
{ key: 5, val: 'world' }
];
var result = arr.map(i => [i.key, i]);
const ze_map = new Map(result);
console.log(JSON.stringify([...ze_map]));