我是打字稿新手。我有一张打字稿地图,如下所示:
const mapping = new Map<string, string>();
mapping.set('fruit', 'apple')
mapping.set('vegetable', 'onion')
...
Run Code Online (Sandbox Code Playgroud)
我正在尝试将映射转换为类型 '{ [key: string]: string; }'
如何在打字稿中做到这一点?
只需执行以下操作:
Object.fromEntries(mapping)
Run Code Online (Sandbox Code Playgroud)
参考:
const mapping = new Map<string, string>()
mapping.set('fruit', 'apple')
mapping.set('vegetable', 'onion')
console.log(mapping)
// { [key: string]: string } is same as Record<string, string>
const record: { [key: string]: string } = Object.fromEntries(mapping)
console.log(record)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3460 次 |
| 最近记录: |