泛型函数重载:"Supplied parameters do not match any signature of call target."



我正在尝试编写一个与以下两个调用兼容的签名。第一个似乎工作正常,但第二个说Supplied parameters do not match any signature of call target..有什么想法吗?

interface I1 {
name: string;
}
interface I2 {
age: number;
}
function set<T, KT extends keyof T>(arg1: KT, arg2?: undefined): (value: T[KT]) => void;
function set<T, KT extends keyof T, U, KU extends keyof U>(arg1: KT, arg2: KU): (value: U[KU]) => void {
// ...
return null;
}
set<I1, "name">("name"); // OK
set<I1, "name", I2, "age">("name", "age"); // ERROR: "Supplied parameters do not match any signature of call target."

这是因为最后一个函数不计入重载候选项中。这是应该处理所有这些问题的实现。

以下作品:

interface I1 {
name: string;
}
interface I2 {
age: number;
}
function set<T, KT extends keyof T>(arg1: KT, arg2?: undefined): (value: T[KT]) => void;
function set<T, KT extends keyof T, U, KU extends keyof U>(arg1: KT, arg2: KU): (value: U[KU]) => null;
function set<T, KT extends keyof T, U, KU extends keyof U>(arg1: KT, arg2: KU): (value: U[KU]) => null {
// ...
return null;
}
const x = set<I1, "name">("name"); // OK
const y = set<I1, "name", I2, "age">("name", "age"); // Now OK

相关内容

最新更新