【发布时间】:2018-08-24 05:30:37
【问题描述】:
给定:
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">?
【问题讨论】:
标签: typescript