【发布时间】:2018-11-27 04:06:17
【问题描述】:
假设我有一个键值对象数组:
const data = [
{key: "object1", value: "data1"},
{key: "object2", value: "data2"},
{key: "object3", value: "data3"},
]
const mappedData = data.map(x => [x.key, x.value]);
const ES6Map = new Map<string, string>(mappedData.values())
如何将其转换为 ES 6 地图?它适用于 JavaScript,但 TypeScript 会抱怨。这里我得到了Argument of type 'IterableIterator<string[]>' is not assignable to parameter of type 'ReadonlyArray<[string, string]>'.
Property 'length' is missing in type 'IterableIterator<string[]>'.的错误
【问题讨论】:
-
只需删除
.values()调用 -
然后得到
Argument of type 'string[][]' is not assignable to parameter of type 'ReadonlyArray<[string, string]>'. Type 'string[]' is not assignable to type '[string, string]'. Property '0' is missing in type 'string[]'. -
问题出在你的类型声明中。我不知道打字稿,但如果类型声明是可选的,为什么不直接做
const ES6Map = new Map(mappedData)?
标签: javascript typescript dictionary ecmascript-6