首页 > 解决方案 > 将元素添加到打字稿 Record<>

问题描述

我有这样的记录设置: let myRecord = Record<String, Set<String>>

如何将元素添加到记录内的集合中?我尝试了以下方法但没有成功:

let key = "key";
let stringToAdd = "stringToAdd";
myRecord[key].add(stringToAdd);

标签: typescriptsetrecord

解决方案


您可以使用方括号[]和赋值向记录添加元素。

let mySet: Set<string> = new Set();
mySet.add("stringToAdd");

let myRecord: Record<string, Set<string>> = {};

myRecord["key"] = mySet;    // <--- Like this

推荐阅读