Skip to main content

57 - 获得必需的属性

Answer TestCases

实现高级工具类型 GetRequired<T>,该类型保留所有必需的属性

例如

type I = GetRequired<{ foo: number; bar?: string }> // expected to be { foo: number }

Solution

type GetRequired<T> = {
[P in keyof T as T[P] extends Required<T>[P] ? P : never]: T[P]
}

看成两部分

(P in keyof T) as (T[P] extends Required<T>[P] ? P : never)

T 等于 { foo: number; bar?: string }

keyof T 等于 'foo' | 'bar', 此时会遍历该联合类型

P'foo', 得到 'foo' as (number extends number ? 'foo' : never), 返回 foo

P'bar', 得到 'bar' as ((string | undefined) extends string ? 'bar' : never) 返回 never

never 时忽略,为 'foo' 时 返回 foo: T['foo'], 即 foo: number