Skip to main content

2852 - OmitByType

Answer TestCases

T 中选择属性,且属性的类型不是 U

例如:

type OmitBoolean = OmitByType<
{
name: string
count: number
isReadonly: boolean
isEnable: boolean
},
boolean
> // { name: string; count: number }

Solution

type OmitByType<T, U> = {
[P in keyof T as T[P] extends U ? never : P]: T[P]
}

遍历 keyof T, 判断属性类型是否属于 U

T[P] extends U ? never : P

属于,则不返回