Record
type A = Record<'name' | 'sex', string>
等价于
interface A{
name: string;
sex: string;
}
Partial 部分类型
interface A {
id: number;
name: string;
}
type B = Partial<A>
等价于
interface B {
id?: number;
name?: string;
}
Required 必填类型
type B = Required<A>
等价于
interface B {
id: number;
name: string;
}
Readonly 只读类型
type B = Readonly<A>
等价于
interface B {
readonly id?: number;
readonly name: string;
}
Exclude 排除类型(用于基础类型)
Exclude<T, U> -- 从T中剔除可以赋值给U的类型。
type A = '男' | '女'
type B = Exclude<A, '女'>
等价于
type B = '男'
Extract 提取类型(用于基础类型)
type A = '男' | '女'
type B = Extract<A, '女'>
等价于
type B = '女'
Pick 提取包含key的类型(用于复杂类型)
type A = {
name: string;
age: number;
height: number;
}
type B = Pick<A, 'name' | 'age'>
等价于
type B = {
name: string;
age: number;
}
Omit 排除包含key的类型(用于复杂类型)
type B = Omit<A, 'name' | 'age'>
等价于
type B = {
height: number;
}
ReturnType 返回值类型
function c () {
return {
name: 'lifa',
age: 18,
height: 180
}
}
type C = ReturnType<typeof c>
等价于
type C = {
name: string;
age: number;
height:number;
}
网友评论