type Taillet<T extends any[]> = ((...t: T) => void) extends ((
h: any,
...r: infer R
) => void)
? R
: never;
type NestedOmit<T, Path extends string[]> = T extends object
? {
0: Omit<T, Path[0]>;
1: {
[K in keyof T]: K extends Path[0] ? NestedOmit<T[K], Taillet<Path>> : T[K];
};
}[Path['length'] extends 1 ? 0 : 1]
: T;
The defined type above functions properly. I have validated it using the following method:
type Test = NestedOmit<{ a: { b: { c: 1 } } }, ['a', 'b', 'c']>;
// {
// a: {
// b: Pick<{
// c: 1;
// }, never>;
// };
// }
However, when implementing this within a function, the expected result is not obtained.
const remove = <T extends object, K extends string[]>(src: T, path: K) => {
// custom logic omitted. only constructing the result below.
const outcome = {
a: {
b: {}
}
}
return result as NestedOmit<T, K>
};
const modifiedObject = remove({a:{b:{c:1}}},["a","b","c"]);
// modifiedObject.a.b.c <========== still works
// desired output is modifiedObject.a.b
Could anyone identify the issue in this scenario?