TypeScript基本知识(7)

58 阅读1分钟
//Partial和Required用法
interface User { 
    address: string
    age: string
    name: string
}

type PartialUser = Partial<User>  // 所有属性变成一个可选的
type RequiredUser = Required<User>  // 所有属性变成一个必选的
type PickUser = Pick<User, 'age'> // 提取部分属性
type ExcludeUser = Exclude<'a' | 'b' | 'c', 'a'> // 排除部分属性
type OmitUser = Omit<User, 'age'> // 排除部分属性 返回新的类型

// 约束对象的key和value
type Key1 = 'c' | 'x' | 'y'
type Value1 = 'cc' | 'xx' | 'yy'

let obj10: Record<Key1, Value1> = {
    c: 'cc',
    x: 'xx',
    y: 'yy'
}