问题 #
实现高级util类型GetOptional<T>
,该类型保留所有可选字段
例如
type case1 = GetOptional<{ foo: number; bar?: string }> // { bar?: string }
type case2 = GetOptional<{ foo: undefined; bar?: undefined }> // { bar?: undefined }
解答 #
type GetOptional<T> = {
[K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K]
}
拆分 #
- 和 Get Required 类似