interface First {
field1: number;
}
interface Second {
field2: number
}
interface Third extends First, Second {
}
// type Forth = Omit<Third, Second>
// expect Fourth to be { field1: number}
Run Code Online (Sandbox Code Playgroud)
通过众所周知的省略类型,我们可以省略类型中的属性
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
Run Code Online (Sandbox Code Playgroud)
例如
Omit<Third, 'field2'> and it will work as the above
Run Code Online (Sandbox Code Playgroud)
但问题是当 Second 有多个字段时
这是可以实现的吗?如何?
如果您想从另一种类型中排除一种类型的所有键,可以使用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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2321 次 |
| 最近记录: |