TypeScript--内置类型1

Partial

描述:Partial 将类型 T 的所有属性标记为可选属性

type Partial<T> = {
    [P in keyof T]?: T[P];
};

示例

interface Person {
    name: string
    age: number
}

type PersonPartial = Partial<Person>

// ✅正确
const p1: PersonPartial = {
    name: 'Tom',
    age: 23,
}

// ✅正确
const p2: PersonPartial = {
    name: 'Tom',
}

// ✅正确
const p3: PersonPartial = {
    age: 23,
}

// ✅正确
const p4: PersonPartial = {}

Required

描述: Required 将类型 T 的所有属性标记为必选属性

type Required<T> = {
    [P in keyof T]-?: T[P];
};

示例

interface Student {
    name?: string
    age?: number
}

type StudentRequired = Required<Student>

// ✅正确
const t1: StudentRequired = {
    name: 'Tom',
    age: 23,
}

// ❌错误
// Property 'age' is missing in type ...
const t2: StudentRequired = {
    name: 'Tom',
}

// ❌错误
// Property 'name' is missing in type ...
const t3: StudentRequired = {
    age: 23,
}

Readonly

描述:Readonly 将类型 T 的所有属性标记为 readonly, 即不能修改。

type Readonly<T> = {
    readonly [P in keyof T]: T[P];
};

示例

interface Person {
    name: string
    age: number
}

type PersonReadonly = Readonly<Person>

const p1: PersonReadonly = {
    name: 'Tom',
    age: 23,
}

// ❌错误
// Cannot assign to 'name' because it is a read-only property.
p1.name = 'Anne'

// ❌错误
// Cannot assign to 'age' because it is a read-only property.
p1.age = 25

Pick

描述:Pick 从类型 T 中过滤出某个或某些属性

type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

示例

interface Person {
    name: string
    age: number
    gender?: 'male' | 'female'
    address?: string
}

// 把 'name' 和 'address' 属性从 'Person' 中过滤出来
type PersonPick = Pick<Person, 'name' | 'address'>

// ✅正确
const p1: PersonPick = {
    name: 'Tom',
    address: 'ShangHai',
}

// ✅正确
const p2: PersonPick = {
    name: 'Tom',
}

// ❌错误
// 'age' does not exist in type 'PersonPick'.
const p3: PersonPick = {
    name: 'Tom',
    age: 23,
}

Record

描述:Record 标记对象的属性(key)或者属性值(value)的类型

type Record<K extends keyof any, T> = {
    [P in K]: T;
};

示例

interface Person {
    name: string
    age: number
    gender?: 'male' | 'female'
    address?: string
}

type PersonRecord = Record<string, Person>

// ✅正确
const r1: PersonRecord = {
    'p': {
        name: 'Tom',
        age: 23,
    }
}

// ❌错误
// Type 'string' is not assignable to type 'Person'.
const r2: PersonRecord = {
    'p': 'Person'
}

// ❌错误
// Property 'age' is missing in type ...
const r3: PersonRecord = {
    'p': {
        name: 'Tom',
    }
}

Exclude

描述:Exclude 从 T 中排除可分配给 U 的类型

type Exclude<T, U> = T extends U ? never : T;

解析:假如 type T = A | B | C,那么会被解析为 (A extends U ? never : A) | (B extends U ? never : B) | (C extends U ? never : C)

示例

// 结果:'b' | 'd'
type StringExclude = Exclude<'a' | 'b' | 'c' | 'd', 'a' | 'c' | 'f'>

// ✅正确
const s1: StringExclude = 'b'

// ✅正确
const s2: StringExclude = 'd'

// ❌错误
// Type '"f"' is not assignable to type 'StringExclude'.
const s3: StringExclude = 'f'

Extract

描述:Extract 从 T 中提取可分配给 U 的类型。Exclude 的反操作。

type Extract<T, U> = T extends U ? T : never;

解析:假如 type T = A | B | C,那么会被解析为 (A extends U ? A : never) | (B extends U ?B : never) | (C extends U ? C : never)

示例

// 结果:'a' | 'c'
type StringExtract = Extract<'a' | 'b' | 'c' | 'd', 'a' | 'c' | 'f'>

// ✅正确
const s1: StringExtract = 'a'

// ✅正确
const s2: StringExtract = 'c'

// ❌错误
// Type '"f"' is not assignable to type 'StringExtract'.
const s3: StringExtract = 'f'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值