typescript - 如何在打字稿中将对象类型转换为联合?
问题描述
例如,我有一个类型 EventMapping,它是一个映射“eventName -> eventData”。我需要将发出的事件存储在列表中。因此我想将此映射转换为事件类型,因此我无法将不正确类型的数据推送到事件列表中。
// type which i have
type EventMapping = {
click: {
position: Point;
};
scroll: {
top: number;
bottom: number;
};
}
// type which i want to get
type Event = {
type: 'click';
data: {
position: Point;
};
} | {
type: 'scroll';
data: {
top: number;
bottom: number;
};
}
// usage example
const events: Event[] = [];
// ok
events.push({
type: 'scroll',
data: {
top: 0,
bottom: 0,
}
});
// error
events.push({
type: 'click',
data: {
top: 0,
bottom: 0,
}
});
解决方案
我的策略是两个步骤:
将 EventMapping 类型转换为键的联合:所以目标是得到
“滚动” | “点击”
这可以通过keyof操作符来完成
从这个联合中,我将它映射到您的事件类型。使用这里提到的技巧:将联合映射到另一个联合类型
结果是:
type Distribute<U> = U extends keyof EventMapping? {type: U, data: EventMapping[U]} : never;
type Event = Distribute<keyof EventMapping>
推荐阅读
- python - module = __import__('.'.join(parts_copy)) ImportError: 不支持按文件名导入
- javascript - RXJS 从流对象属性中获取逗号分隔列表
- javascript - 如何使用扩展语法从 JavaScript 中的多个未知对象复制对象属性?
- google-apps-script - 如何将某个列值重置为零?
- html - 根据谷歌表格单元格中的值创建一个水平条
- html - 使用 CSS 使相邻的 div 占据所有垂直空间
- php - Sql - 重复的变量输入到 Mysql
- python - 为什么在使用应用 lambda 时出现错误“浮动”对象不可迭代
- java - Maven 的 JAVA_HOME 环境变量分号问题
- ios - UITableViewCell 动画被击中或错过。有时表格数据/单元格会在没有动画的情况下弹出