【发布时间】:2021-12-12 16:19:53
【问题描述】:
我有一个旧版 API,如下所示 (playground link)...
type Command1 = {
cmd: "my first command",
arg1: string,
arg2: boolean
}
type Command2 = {
cmd: "my second command",
foo: string,
bar: number
}
type Command = Command1 | Command2
function execute(cmd: Command["cmd"], args:any /* would like to strongly type this */) {
console.log(args)
}
execute("my first command", {/* oops missing props */})
有没有什么方法可以在不改变函数参数列表的情况下对execute函数的args参数进行类型检查?
谢谢
【问题讨论】:
-
你能展示一些
execute()被正确调用的例子吗?不清楚您打算如何将Command的属性映射到args参数。 -
参见@tokland 的回答,他成功了