typescript - 您如何定义具有键字符串索引但具有特定类型的已知键的 TypeScript 接口?
问题描述
您如何定义具有 Key String 索引但具有特定类型的已知键的 TypeScript 接口,以便您可以在映射中引用该键?
示例界面(这不起作用)
interface column {
label?: string;
width: string;
}
export interface IColumns {
[key: string]: {
title: column;
objectID?: number;
url?: column;
author: column;
comments: column;
points: column;
archive: column;
};
}
这是这样使用的,
const COLUMNS : IColumns = {
title: {
label: "Title",
width: "40%",
},
author: {
label: "Author",
width: "30%",
},
comments: {
label: "Comments",
width: "10%",
},
points: {
label: "Points",
width: "10%",
},
archive: {
width: "10%",
},
};
这是我映射转换后的对象以及引用密钥的地方。
const Stories: React.FC<{ stories: IStory[] }> = ({ stories }) => (
<div className="stories">
<div className="stories-header">
{Object.keys(COLUMNS).map((key) => (
<span key={key} style={{ width: COLUMNS[key].width }}>
{COLUMNS[key].label}
</span>
))}
</div>
解决方案
您可以为可能的键声明一个类型
type Prop = 'title' | 'author' | 'comments' | 'points' | 'archive';
然后接口将使用in
关键字将该类型用作计算键
type IColumns = {[key in Prop]: column}
现在您可以使用该界面
const COLUMNS : IColumns = {
title: {
label: "Title",
width: "40%",
},
author: {
label: "Author",
width: "30%",
},
comments: {
label: "Comments",
width: "10%",
},
points: {
label: "Points",
width: "10%",
},
archive: {
width: "10%",
},
};
关于地图,您有 2 个选项:
- 使用
Object.keys
但将密钥转换为Prop
(Object.keys(COLUMNS) as Array<Prop>).forEach(key => ...)
- 使用
Object.entries
所以value
将已经Prop
Object.entries(COLUMNS).map(([key, value /* :Prop */])
推荐阅读
- node.js - 温斯顿:加密敏感信息
- apache - 如何使用具有 JNDI 属性的 Apache MQ 配置分布式主题或队列,并将 MQ 嵌入应用服务器
- json - 使用 jq 向 json 数组中的每个对象添加一个新的键/值
- c# - 使用原生 win32 gdi 绘制透明线
- acumatica - DAC 字段上的 PXSelector 导致列过滤器问题
- excel - Vlookup 在不同的行中返回多个匹配值
- c# - C#在命中测试上添加圆圈
- angular - 在 Apache 的单根文件夹中运行多个 Angular 6 项目
- powershell - 在 Powershell 中使用 itextsharp 从 PDF 中提取页面
- javascript - 网络音频 - decodeAudioData