1. typeof

在 TypeScript中,typeof 操作符可以用来获取一个变量的声明,或是对象的类型

  1. //例子1
  2. interface People {
  3. name: string;
  4. age: number;
  5. }
  6. const variableDai: People = { name: 'coolFish', age: 24 };
  7. type formDai= typeof variableDai; // -> People
  8. //例子2
  9. function toArray(x: number): Array<number> {
  10. return [x];
  11. }
  12. type Func = typeof toArray; // -> (x: number) => number[]

小结:要注意的是,例子1中我们定义了一个常量 variableDai 然后获取他的类型,赋值给了类型 formDai。

2. keyof

keyof 操作符可以用来一个对象中的所有 key 值, 返回的是这些key值得联合类型。

  1. interface Person {
  2. name: string;
  3. age: number;
  4. }
  5. type allKey = keyof Person; // "name" | "age"

当然我们可以利用这个特性来做一些特殊的操作,例如遍历一个数组,返回一个数组所拥有的所有属性的联合类型

  1. interface Person {
  2. name: string;
  3. age: number;
  4. }
  5. type key = keyof Person[];// "length" | "toString" | "pop" | "push" | "concat" | "join"
  1. type K3 = keyof { [x: string]: Person }; // string | number
  2. // 因为这里的键是索引类型,所以是 string | number

3. in

in 用来遍历枚举类型

  1. type Keys = 'a'|'b'|'c'
  2. type obj = {
  3. [p in Keys]: any
  4. }
  5. //{ a: any, b: any, c: any }

4. infer

在条件类型语句中,可以用infer 声明一个类型变量,并且对它进行使用。

  1. //返回数组的第一项
  2. type Head<T extends Array<any>> = T extends [head : infer H, ...rest : any[]] ? H : never;
  3. // 测试用例
  4. type H0 = Head<[]> // never
  5. type H1 = Head<[1]> // 1
  6. type H2 = Head<[3, 2]> // 3

上例的原理是,我们用infer定义一个类型变量为第一项,然后类型收窄,就获得了第一项。

5. extends

当我们定义的泛型不想过于灵活,或者想继承某些类的时候,我们可以通过 extends 关键字添加泛型约束

  1. interface mustLength {
  2. length: number;
  3. }
  4. function mustTakeLength<T extends mustLength>(arg: T): T {
  5. console.log(arg.length);
  6. return arg;
  7. }
  8. mustTakeLength(3)// Error, number doesn't have a .length property
  9. loggingIdentity({length: 10, value: 3});//10

我们定义了一个 mustLength 的接口,然后 入参受该接口约束,必须有一个 length 属性。否则就会报错

  1. type User = {
  2. id: number;
  3. kind: string;
  4. };
  5. // T assignable User
  6. function makeCustomer<T extends User>(u: T): T {
  7. return {
  8. ...u,
  9. id: u.id,
  10. kind: 'customer'
  11. }
  12. }
  13. // 用扩展运算符将多余参数返回即可。

6. Partial

Partial 的作用就是将某个类型里的属性全部变成可选,该方法是联合了 keyof 和 in实现的。

  1. type Partial<T> = {
  2. [P in keyof T]?: T[P];
  3. };
  4. interface User {
  5. name:string,
  6. age:number,
  7. department:string
  8. }
  9. type optional = Partial<User>
  10. /**type optional = {
  11. name?: string | undefined;
  12. age?: number | undefined;
  13. department?: string | undefined;
  14. }**/

7. Required

Required 的作用就是将某个类型中的属性全部变为必选,具体实现和Partial类似

8. Readonly

Readonly 的作用是将某个类型所有属性变为只读属性,也就意味着这些属性不能被重新赋值。

9. Record

Record 的作用是将 K 中所有的属性的值转换为 T 类型

  1. type Record<K extends keyof any, T> = {
  2. [P in K]: T;
  3. };
  4. // 将K 中有类型的属性进行遍历,再将每个 Key 赋值 T类型 实例
  1. type petsGroup = 'dog' | 'cat' | 'fish';
  2. interface IPetInfo {
  3. name:string,
  4. age:number,
  5. }
  6. type IPets = Record<petsGroup, IPetInfo>;
  7. const animalsInfo:IPets = {
  8. dog:{
  9. name:'dogName',
  10. age:2
  11. },
  12. cat:{
  13. name:'catName',
  14. age:3
  15. },
  16. fish:{
  17. name:'fishName',
  18. age:5
  19. }
  20. }