interface First {
field1: number;
}
interface Second {
field2: number
}
interface Third extends First, Second {
}
// type Forth = Omit<Third, Second>
// expect Fourth to be { field1: number}
使用众所周知的Omit类型,我们可以从类型中省略属性
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
例如
Omit<Third, 'field2'> and it will work as the above
但是问题是,当Second具有多个字段时
这可以实现吗?怎么样?
答案 0 :(得分:2)
如果要将一种类型的所有键从另一种类型中排除,则可以使用keyof
作为Omit
的参数:
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
interface First {
field1: number;
}
interface Second {
field2: number
}
interface Third extends First, Second {
}
type ThirdWithoutSecond = Omit<Third, keyof Second>