const a = ['a', 'b'] as const
// b: 'a' | 'b'
const b: typeof a[number]
TypeScript 中内置了 Partial 类型方法,但是很多时候我们需要对嵌套对象使用,此时 Partial 便不能满足我们的需求。
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer I>
? Array<DeepPartial<I>>
: DeepPartial<T[P]>
}
// 测试
interface A {
user: {
age: number
name: string
}
}
type B = DeepPartial<A>