typescript - TypeScript 中的字典类型
问题描述
在我的应用程序的几个地方,我声明了一个字典类型,例如:
interface MyInterface {
data: { [key: string]: Item };
}
TypeScript 中是否有任何内置的字典/地图简写,以获得类似于:
interface MyInterface {
data: Dict<Item>;
}
解决方案
我们可以尝试使用名为的内置打字稿高级类型Record<K, T>
interface MyInterface {
data: Record<string, Item>;
}
把所有东西放在一起
interface Item {
id: string;
name: string;
}
interface MyInterface {
data: Record<string, Item>;
}
const obj: MyInterface = {
data: {
"123": { id: "123", name: "something" }
}
};
推荐阅读
- python - Django 令牌和会话身份验证
- ios - Apple 登录 TVOS 问题 -7014
- ios - 我应该如何为 swift 项目构建我的存储库?
- python - 了解使用不同宽度的高斯进行卷积/反卷积时的行为
- javascript - 在反应原生博览会上,图像未显示在 Android 设备上
- html - 我怎样才能添加有角度的边框或类似这个网站的东西?
- amazon-web-services - 地形导入 aws_cloudwatch_log_stream
- java - Java:如何将字符串中每个单词的第一个字符大写,并正确大写名称
- javascript - 表单提交将用户带到另一个页面后,Window.history.state 为空
- excel - 按数字顺序对 Excel 列进行排序