相关疑难解决方法(0)

用于删除特定嵌套路径处的对象的类型

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;
Run Code Online (Sandbox Code Playgroud)

我有上面的类型,效果很好。我已经这样测试过:

type A = DeepOmit<{ a: { b: { c: 1 } } }, ['a', 'b', 'c']>;

// { …
Run Code Online (Sandbox Code Playgroud)

typescript typescript-generics

0
推荐指数
1
解决办法
470
查看次数

标签 统计

typescript ×1

typescript-generics ×1