Previously
Type any:allow anything unknown:ensure someone using this type declare what the type is never:it is not possible that this type could happen void:a function which returns undefined or has no return value
// type 关键字用来定义一种类型(给类型组合取一个别名)type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE'let method: Methodsmethod = 'PUT' // OKmethod = 'aaa' // error
// 元组 类型数量确定的数组type source = [number, string?];const pointX: source = [11, '33']; // OKconst pointY: source = [11]; // OK
Base
元组
元组类型,是另一种Array类型,它确切的包含了多少元素,已经它在特定位置包含哪些类型 因此可以检查length得到确切数字
const fullName:[first: string, last: string] = ['hello', 'world'];const range:[start: number, end: number] = [0, 10];const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as const;type FullNameLength = (typeof fullName)['length'] // 2type RangeLength = (typeof range)['length'] // 2type DigitsLength = (typeof digits)['length'] // 10// 元组类型可以通过T[number]从元组中获取值// 约束泛型的类型保证不会出现报错/*** 这里的extends readonly any[] 是调用T[number] 所必须的,* 用来约束 T 的类型,T是一个元组,元组元素是只读的*/type TupleToObject<T extends readonly any[]> = {[Value in T[number]]: Value;};
数组类型就不可以
const fullName:string[] = ['hello', 'world'];const range:number[] = [0, 10];type FullNameLength = (typeof fullName)['length'] // numbertype RangeLength = (typeof range)['length'] // number
内置类型
Partial(属性转换为可选)
将传入类型中的属性变为可选
type Partial<T> = {[P in keyof T]?: T[P]}interface Person {name: string,age: number}/*** Type '{}' is not assignable to type 'Student'* Property 'name' is missing in type '{}'.*/const student: Person = {};const cat:Partial<Person> = {}; // ok
Required(属性转换为必选)
将传入类型中的属性变为必选
type Required<T> = {[P in keyof T]-?: T[P]}interface Pet {age?: number,name?: string}const cat: Pet = {} // ok/*** Type '{}' is not assignable to type 'Required<Pet>'.* Property 'age' is missing in type '{}'.*/const sutdent: Required<Pet> = {}
Readonly
将传入的类型变为只读
type Readonly<T> = {readonly[P in keyof T]: T[P]}interface Info {name: string,age: number,grade: number,}const teacher: Info = {name: 'zero',age: 18,grade: 90}const student: Readonly<Info> = {name: 'Lu',age: 18,grade: 59}teacher.grade = 99; // ok/** Cannot assign to 'grade' because it is a constant or a read-only property.*/student.grade = 99;
Pick(留下联合类型中给定属性的类型)
将传入类型中的部分属性组成新的类型
type Pick<T, K extends keyof T> = {[P in K]: T[P]}interface Info {male:{age: number},female:{height: number}}/** Type '{ female: { height: number; }; }'* is not assignable to type 'Pick<Info, "male">'.* Object literal may only specify known properties,* but 'female' does not exist in type 'Pick<Info, "male">'.* Did you mean to write 'male'?*/const Lu: Pick<Info, 'male'> = {female:{height: 150}}// okconst Lu_M: Pick<Info, 'male'> = {male:{age: 18}}
Record(给联合类型中每一个属性规定类型)
构造一个类型,该类型的属性严格相等于传入类型 Record应该是日常使用频率较高的内置类型了,主要用来描述对象,一般建议是不用Object来描述对象,而是用Record代替,Record
几乎可以说是万金油了
type Record<K extends keyof any, T> = {[P in K]: T}type pets = 'cat' | 'dog' | 'fish'interface Pet {name: string,age: number}const family: Record<pets,Pet> = {// okcat:{name: 'white',age: 2},/*** Type '{ name: string; }' is not assignable to type 'Pet'.* Property 'age' is missing in type '{ name: string; }'.*/dog:{name: 'black'},/*** Type '{}' is not assignable to type 'Pet'.* Property 'name' is missing in type '{}'.*/fish:{}}
Exclude(去除联合类型中不同属性)
针对联合类型,去除联合类型中指定类型
type Exclude<T, U> = T extneds U ? never : T;type Pets = 'dog' | 'cat' | 'fish' | 'wolf' | 'bird'type Wild = 'wolf' | 'bird'const familyM1: Exclude<Pets, Wild> = 'cat' // ok/*** Type '"wolf"' is not assignable to type '"dog" | "cat" | "fish"'.*/const familyM2: Exclude<Pets, Wild> = 'wolf'
Extract(留下联合类型中相同属性)
针对联合类型,留下相同类型
type Extract<T, U> = T extends U ? T : never;type zero = 'name' | 'age' | 'female'type Lu = 'name' | 'age' | 'male'const person1: Extract<Lu, zero> = 'name' // okconst person2: Extract<Lu, zero> = 'age' // ok/*** Type '"male"' is not assignable to type '"name" | "age"'.*/const person3: Extract<Lu, zero> = 'male'/*** Type '"female"' is not assignable to type '"name" | "age"'.*/const person4: Extract<Lu, zero> = 'female'
Omit(省去类型中联合类型指定属性)
在T中省去K(联合类型),将留下的类型组成新类型
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;interface People {name: string,age: number,job: string}type Person = 'age' | 'job'const Lu: Omit<People, Person> = {name: 'Lu', // ok/*** Type '{ name: string; age: number; }'* is not assignable to type 'Omit<People, Person>'.* Object literal may only specify known properties,* and 'age' does not exist in type 'Omit<People, Person>'.*/age: 18}
