我想知道如何正确推断我的函数的第二个和第三个模板
假设一个简单的界面
interface ISome {
a: string;
b?: {
c: string;
};
}
Run Code Online (Sandbox Code Playgroud)
关注作品
function pathBuilder<
K1 extends keyof ISome,
K2 extends keyof NonNullable<ISome[K1]>>(p: K1, p2?: K2) {
let res = String(p);
if (p2) { res += "." + p2; }
return res;
}
const pathTest = pathBuilder("b", "c"); // ---> "b.c" and intellisense works on parameters
Run Code Online (Sandbox Code Playgroud)
但我需要通过指定另一种类型来概括该函数以使其工作(我不想传递对象实例来指定类型)
所以,以下不起作用
function pathBuilder<
T,
K1 extends keyof T,
K2 extends keyof NonNullable<T[K1]>>(p: K1, p2?: K2) {
let res = String(p); …
Run Code Online (Sandbox Code Playgroud)