Appearance
Extract
从一个联合类型中返回包含 K 包含的字符串的类型的联合类型
ts
type Extract<T, U> = T extends U ? T : never
重点
Extract 是作用于属性的(keyof),而不是作用于类型的(typeof)
其返回的是一个联合类型(
"a" | "b"
)U 中可以包含不存在于 T 中的类型
栗子
ts
const person = {
name: "张三",
age: 12,
address: "南京",
}
type Person = typeof person
// Extract 是作用于属性的
type PersonKeys = keyof Person
// "name" | "age"
type MiniPerson = Extract<keyof typeof person, "name" | "age" | "sex">