ts 泛型 高级类型

  1. typeof
    获取一个变量声明或对象的类型
interface User{
  name: string;
  age: number;
}

const user: User= { name: 'huxianc', age: 25 };
type UserType= typeof user; // -> User

function func(x: number): number[] {
  return [x];
}

type Func = typeof func; // -> (x: number) => number[]

  1. keyof
    获取某种类型的所有键,返回联合类型
interface User{
  name: string;
  age: number;
}

type K1 = keyof User; // "name" | "age"
type K2 = keyof User[]; // number | "length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | ... 14 more ... | "includes"
// 对象的propName就算是number也会转换成string
type K3 = keyof { [x: string]: User};  // string | number
  1. in
    遍历枚举类型
export interface INavOBj {
  name: string;
  code: string;
  title?: string | undefined;
}

type NavBasicProps = "a" | "b" | "c";
export interface INavBasic {
  [p in NavBasicProps]: INavOBj;
}
  1. infer
    在条件类型语句中,可以用 infer 声明一个类型变量并且对它进行使用
type ReturnType<T> = T extends (
  ...args: any[]
) => infer R ? R : any;

以上代码中 infer R 就是声明一个变量来承载传入函数签名的返回值类型,简单说就是用它取到函数返回值的类型方便之后使用

  1. extends
    泛型约束
interface Lengthwise {
  length: number;
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
  console.log(arg.length);
  return arg;
}
  1. Partial
    Partial<T> => T中所有属性变成可选属性?
type Partial<T> = {
  [P in keyof T]?: T[P];
};
  1. Required
    和上面相反,所有变为必选
  2. Readonly
    所有变成只读
  3. Pick<T, K extends keyof T>
    从泛型 T 中检出指定的属性并组成一个新的对象类型
type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

interface IUser {
  name: string;
  age: number;
  address: string;
}

type t = Pick<IUser, 'name' | 'age'>;
/* type t = {
	name: string;
	age: number
} */
  1. Record<K extends keyof any, T>
    Record 允许从 Union 类型中创建新类型,Union 类型中的值用作新类型的属性
type Record<K extends keyof any, T> = {
  [P in K]: T;
};

type Car = 'Audi' | 'BMW' | 'MercedesBenz'
type CarList = Record<Car, {age: number}>

const cars: CarList = {
  Audi: { age: 119 },
  BMW: { age: 113 },
  MercedesBenz: { age: 133 },
}
/* type CarList = {
      Audi: {
          age: number;
      };
      BMW: {
          age: number;
      };
      MercedesBenz: {
          age: number;
      };
}*/
  1. Exclude<T, U>
    从泛型 T 中排除可以赋值给泛型 U 的类型
type Exclude<T, U> = T extends U ? never : T;

type a = 1 | 2 | 3;

type t = Exclude<a, 1 | 2>;
/* type t = 3 */
  1. Extract<T, U>
    从泛型 T 中提取可以赋值给泛型 U 的类型
type Extract<T, U> = T extends U ? T : never;

type a = 1 | 2 | 3;

type t = Extract<a, 1 | 2>;
/* type t = 1 | 2 */
  1. Omit<T, K extends keyof any>
    从泛型 T 中提取出不在泛型 K 中的属性类型,并组成一个新的对象类型
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

interface IUser {
  name: string;
  age: number;
  address: string;
}

type t = Omit<IUser, 'name' | 'age'>;
/* type t = {
	address: string;
} */
  1. NonNullable<T>
    从泛型 T 中排除掉 null 和 undefined
type NonNullable<T> = T extends null | undefined ? never : T;

type t = NonNullable<'name' | undefined | null>;
/* type t = 'name' */
  1. ReturnType<T extends (...args: any) => any>
    获得函数返回值的类型
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

type t = ReturnType<(name: string) => string | number>
// type t = string | number
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页