type Tail<T extends any[]> = ((...t: T) => void) extends ((
h: any,
...r: infer R
) => void)
? R
: never;
type DeepOmit<T, Path extends string[]> = T extends object
? {
0: Omit<T, Path[0]>;
1: {
[K in keyof T]: K extends Path[0] ? DeepOmit<T[K], Tail<Path>> : T[K];
};
}[Path['length'] extends 1 ? 0 : 1]
: T;
I have the above type which works well. I have tested it this way:
type A = DeepOmit<{ a: { b: { c: 1 } } }, ['a', 'b', 'c']>;
// {
// a: {
// b: Pick<{
// c: 1;
// }, never>;
// };
// }
However when I add this inside a function, I dont get the desired output.
const del = <T extends object, K extends string[]>(src: T, path: K) => {
// custom logic removed. just creating the result below.
const result = {
a: {
b: {}
}
}
return result as DeepOmit<T, K>
};
const deletedObj = del({a:{b:{c:1}}},["a","b","c"]);
// deletedObj.a.b.c <========== still works
// expected is deletedObj.a.b
Can someone point me what is the issue here ?