typescript - 为什么在这种情况下类型缩小会失败?
问题描述
鉴于:
export type CommandsTypes = {
"test-command": {
propA: string;
propB: number;
};
"test-command2": {
propC: string;
propD: number;
};
};
export type Command<K extends keyof CommandsTypes> = {
type: K;
payload: CommandsTypes[K];
};
export type AnyCommand = Command<keyof CommandsTypes>;
为什么以下内容没有按预期缩小类型:
function handle(command: AnyCommand) {
if (command.type === "test-command") {
// I would expect the type of command at this point to be
// Command<"test-command">
// But its not?
}
}
任何想法为什么 Typescript 无法将AnyCommand
上面的类型缩小到
Command<"test-command">
?
解决方案
Command<keyof CommandTypes>
基本上相当于{ type: keyof CommandTypes, payload :CommandTypes[keyof CommandTypes] }
这意味着您可以将任何类型与任何有效负载配对,这不是您想要的。
你会想建立一个有区别的工会。为此,我们可以使用条件类型的分配行为,它将对键联合的每个成员应用类型转换
export type AnyCommandHelper<T extends keyof CommandsTypes> =
T extends keyof CommandsTypes? Command<T>: never
export type AnyCommand = AnyCommandHelper<keyof CommandsTypes>;
推荐阅读
- html - 将 ul、li 等代码放入外部 CSS 样式表
- c# - 为最后一个 foreach 值替换的工具提示值
- python - 如何从相关矩阵和波动率向量创建协方差矩阵?
- python - 如何通过 Google App Engine 从 eBay Finding API 获取数据(项目)?
- c++ - C++ 可以在全局范围内有代码吗?
- c# - 添加服务引用后如何部署asp.net应用程序而不构建解决方案
- ocaml - 如何在 OCaml 中简洁地解开代数数据类型?
- redis - 根据mysql的每日提取更新redis数据
- ios - 如何在 Swift 中获取解析服务器时间?
- php - 如何将每个答案存储在答案表中?(如何正确存储在数组中)