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

  1. // type 关键字用来定义一种类型(给类型组合取一个别名)
  2. type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE'
  3. let method: Methods
  4. method = 'PUT' // OK
  5. method = 'aaa' // error
  1. // 元组 类型数量确定的数组
  2. type source = [number, string?];
  3. const pointX: source = [11, '33']; // OK
  4. const pointY: source = [11]; // OK

Base

元组

元组类型,是另一种Array类型,它确切的包含了多少元素,已经它在特定位置包含哪些类型 因此可以检查length得到确切数字

  1. const fullName:[first: string, last: string] = ['hello', 'world'];
  2. const range:[start: number, end: number] = [0, 10];
  3. const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as const;
  4. type FullNameLength = (typeof fullName)['length'] // 2
  5. type RangeLength = (typeof range)['length'] // 2
  6. type DigitsLength = (typeof digits)['length'] // 10
  7. // 元组类型可以通过T[number]从元组中获取值
  8. // 约束泛型的类型保证不会出现报错
  9. /**
  10. * 这里的extends readonly any[] 是调用T[number] 所必须的,
  11. * 用来约束 T 的类型,T是一个元组,元组元素是只读的
  12. */
  13. type TupleToObject<T extends readonly any[]> = {
  14. [Value in T[number]]: Value;
  15. };

数组类型就不可以

  1. const fullName:string[] = ['hello', 'world'];
  2. const range:number[] = [0, 10];
  3. type FullNameLength = (typeof fullName)['length'] // number
  4. type RangeLength = (typeof range)['length'] // number

内置类型

Partial(属性转换为可选)

将传入类型中的属性变为可选

  1. type Partial<T> = {
  2. [P in keyof T]?: T[P]
  3. }
  4. interface Person {
  5. name: string,
  6. age: number
  7. }
  8. /**
  9. * Type '{}' is not assignable to type 'Student'
  10. * Property 'name' is missing in type '{}'.
  11. */
  12. const student: Person = {};
  13. const cat:Partial<Person> = {}; // ok

Required(属性转换为必选)

将传入类型中的属性变为必选

  1. type Required<T> = {
  2. [P in keyof T]-?: T[P]
  3. }
  4. interface Pet {
  5. age?: number,
  6. name?: string
  7. }
  8. const cat: Pet = {} // ok
  9. /**
  10. * Type '{}' is not assignable to type 'Required<Pet>'.
  11. * Property 'age' is missing in type '{}'.
  12. */
  13. const sutdent: Required<Pet> = {}

Readonly

将传入的类型变为只读

  1. type Readonly<T> = {
  2. readonly[P in keyof T]: T[P]
  3. }
  4. interface Info {
  5. name: string,
  6. age: number,
  7. grade: number,
  8. }
  9. const teacher: Info = {
  10. name: 'zero',
  11. age: 18,
  12. grade: 90
  13. }
  14. const student: Readonly<Info> = {
  15. name: 'Lu',
  16. age: 18,
  17. grade: 59
  18. }
  19. teacher.grade = 99; // ok
  20. /*
  21. * Cannot assign to 'grade' because it is a constant or a read-only property.
  22. */
  23. student.grade = 99;

Pick(留下联合类型中给定属性的类型)

将传入类型中的部分属性组成新的类型

  1. type Pick<T, K extends keyof T> = {
  2. [P in K]: T[P]
  3. }
  4. interface Info {
  5. male:{
  6. age: number
  7. },
  8. female:{
  9. height: number
  10. }
  11. }
  12. /*
  13. * Type '{ female: { height: number; }; }'
  14. * is not assignable to type 'Pick<Info, "male">'.
  15. * Object literal may only specify known properties,
  16. * but 'female' does not exist in type 'Pick<Info, "male">'.
  17. * Did you mean to write 'male'?
  18. */
  19. const Lu: Pick<Info, 'male'> = {
  20. female:{
  21. height: 150
  22. }
  23. }
  24. // ok
  25. const Lu_M: Pick<Info, 'male'> = {
  26. male:{
  27. age: 18
  28. }
  29. }

Record(给联合类型中每一个属性规定类型)

构造一个类型,该类型的属性严格相等于传入类型 Record应该是日常使用频率较高的内置类型了,主要用来描述对象,一般建议是不用Object来描述对象,而是用Record代替,Record几乎可以说是万金油了

  1. type Record<K extends keyof any, T> = {
  2. [P in K]: T
  3. }
  4. type pets = 'cat' | 'dog' | 'fish'
  5. interface Pet {
  6. name: string,
  7. age: number
  8. }
  9. const family: Record<pets,Pet> = {
  10. // ok
  11. cat:{
  12. name: 'white',
  13. age: 2
  14. },
  15. /**
  16. * Type '{ name: string; }' is not assignable to type 'Pet'.
  17. * Property 'age' is missing in type '{ name: string; }'.
  18. */
  19. dog:{
  20. name: 'black'
  21. },
  22. /**
  23. * Type '{}' is not assignable to type 'Pet'.
  24. * Property 'name' is missing in type '{}'.
  25. */
  26. fish:{}
  27. }

Exclude(去除联合类型中不同属性)

针对联合类型,去除联合类型中指定类型

  1. type Exclude<T, U> = T extneds U ? never : T;
  2. type Pets = 'dog' | 'cat' | 'fish' | 'wolf' | 'bird'
  3. type Wild = 'wolf' | 'bird'
  4. const familyM1: Exclude<Pets, Wild> = 'cat' // ok
  5. /**
  6. * Type '"wolf"' is not assignable to type '"dog" | "cat" | "fish"'.
  7. */
  8. const familyM2: Exclude<Pets, Wild> = 'wolf'

Extract(留下联合类型中相同属性)

针对联合类型,留下相同类型

  1. type Extract<T, U> = T extends U ? T : never;
  2. type zero = 'name' | 'age' | 'female'
  3. type Lu = 'name' | 'age' | 'male'
  4. const person1: Extract<Lu, zero> = 'name' // ok
  5. const person2: Extract<Lu, zero> = 'age' // ok
  6. /**
  7. * Type '"male"' is not assignable to type '"name" | "age"'.
  8. */
  9. const person3: Extract<Lu, zero> = 'male'
  10. /**
  11. * Type '"female"' is not assignable to type '"name" | "age"'.
  12. */
  13. const person4: Extract<Lu, zero> = 'female'

Omit(省去类型中联合类型指定属性)

T中省去K(联合类型),将留下的类型组成新类型

  1. type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
  2. interface People {
  3. name: string,
  4. age: number,
  5. job: string
  6. }
  7. type Person = 'age' | 'job'
  8. const Lu: Omit<People, Person> = {
  9. name: 'Lu', // ok
  10. /**
  11. * Type '{ name: string; age: number; }'
  12. * is not assignable to type 'Omit<People, Person>'.
  13. * Object literal may only specify known properties,
  14. * and 'age' does not exist in type 'Omit<People, Person>'.
  15. */
  16. age: 18
  17. }