首页 > 解决方案 > 使用打字稿进行深度省略

问题描述

是否可以在一个函数上保持类型覆盖,从而深度删除对象中键的所有实例?

我的功能看起来像这样。

function omitDeep<T extends object>(obj: T, key: string): TWithoutProvidedKey {
  return JSON.parse(
    JSON.stringify(obj),
    (key: string, value: any) => key === "__typename" ? undefined : value
  );
}

有什么办法可以实现TWithoutProvidedKey吗?

标签: typescript

解决方案


这很容易做到,您只需要使用映射类型来递归属性:

type Primitive = string | Function | number | boolean | Symbol | undefined | null 
type DeepOmitHelper<T, K extends keyof T> = {
    [P in K]: //extra level of indirection needed to trigger homomorhic behavior 
        T[P] extends infer TP ? // distribute over unions
        TP extends Primitive ? TP : // leave primitives and functions alone
        TP extends any[] ? DeepOmitArray<TP, K> : // Array special handling
        DeepOmit<TP, K> 
        : never
}
type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T,Exclude<keyof T, K>> 

type DeepOmitArray<T extends any[], K> = {
    [P in keyof T]: DeepOmit<T[P], K>
}
type Input =  {
    __typename: string,
    a: string,
    nested: {
        __typename: string,
        b: string
    }
    nestedArray: Array<{
        __typename: string,
        b: string
    }>
    nestedTuple: [{
        __typename: string,
        b: string
    }]
}

type InputWithoutKey = DeepOmit<Input, '__typename'>

let s: InputWithoutKey = {
    a: "",
    nested: {
        b:""
    },
    nestedArray: [
        {b: ""}
    ],
    nestedTuple: [
        { b: ""},
    ]
}

只是一个警告,这适用于 3.4,数组和元组上映射类型的处理最近发生了变化,因此根据版本,您可能需要将数组作为特殊情况处理。


推荐阅读