Record
Record的内部定义,接收两个泛型参数;Record后面的泛型就是对象键和值的类型
作用:义一个对象的 key 和 value 类型
源码
Record
Record
Record
{} 任何不为空的对象
type Record
逐步解析
泛型K即为第一次参数
p in xx 又是什么意思呢?
in的意思就是遍历,如上就是将 类型string进行遍历,也就是string
每个属性都是传入的T类型,如 string: PersonModel
Partial
源码
type Partial
作用:生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为可选项
interface Foo { name: string age: number } type Bar = Partial
Required
源码
type Required
作用:生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为必选项
interface Foo { name: string age?: number } type Bar = Required
Readonly
源码
type Readonly
作用:生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为必选项
interface Foo { name: string age: number } type Bar = Readonly
Pick
源码
type Pick
作用:生成一个新类型,该类型拥有 T 中的 K 属性
interface Foo { name: string age?: number gender: string } type Bar = Pick
Exclude
源码
type Exclude
作用:如果 T 是 U 的子类型则返回 never 不是则返回 T
type A = number | string | boolean type B = number | boolean type Foo = Exclude // 相当于 type Foo = string
Extract
源码
type Extract
作用: 和 Exclude 相反
type A = number | string | boolean type B = number | boolean type Foo = Exclude // 相当于 type Foo = number | boolean
Qmit
源码
type Omit
作用:生成一个新类型,该类型拥有 T 中除了 K 属性以外的所有属性
type Foo = { name: string age: number } type Bar = Omit
NonNullable
源码
NonNullable
作用:从泛型 T 中排除掉 null 和 undefined
type NonNullable
Parameters
源码
Parameters
作用:以元组的方式获得函数的入参类型
type Parameters
ConstructorParameters
源码
ConstructorParameters
作用:以元组的方式获得构造函数的入参类型
type ConstructorParameters
ReturnType
源码
ReturnType
作用:获得函数返回值的类型
type ReturnType
InstanceType
源码
InstanceType
作用:获得构造函数返回值的类型
type InstanceType
总结
1、重点理解这些内置的工具类型的定义。
2、能够以此为参考写一些工具类型并运用到项目中去。
