TypeScript 提供了一些实用的公共使用类型,这些工具类型是放在全局中的

Partial<Type>

可使用版本: 2.1

这个工具会返回一个指定类型的子集

type Type1 = {
  test1: string;
  test2: string;
};

type Type2 = Partial<Type1>; // type Type2 = { test1 ?: string, test2 ?: string }

Required<Type>

可使用版本: 2.8

使指定类型的所有属性变为必须实现

type Type1 = {
  test1?: string;
  test2?: string;
};

type Type2 = Requires<Type1>; // type Type2 = { test1: string, test2: string }

Readonly<Type>

可使用版本: 2.1

将指定类型变为只读

type Type1 = {
  test1: string;
  test2: string;
};

type Type2 = Readonly<Type1>; // type Type2 = { readonly test1: string, readonly test2: string }

Record<keys, Type>

可使用版本: 2.1

生成一个值类型为Type对象类型

type keys = 'test1' | 'test2';

type Obj = Record<keys, string>; // type Obj = { test1: string, test2: string }

Pick<Type, keys>

可使用版本: 2.1

在指定的类型中选择指定的 key,并返回一个新对象

type keys = 'test1' | 'test2';

type Obj = {
  test1: string;
  test2: string;
  test3: string;
};

type newObj = Pick<Obj, keys>; // type newObj = { test1: string, test2: string }

Omit<Type, Keys>

可使用版本: 3.5

在一个指定类型中删除指定的 key,并返回一个新对象

type keys = 'test3';

type Obj = {
  test1: string;
  test2: string;
  test3: string;
};

type newObj = Omit<Obj, keys>; // type newObj = { test1: string, test2: string }

Exclude<UnionType, ExcludeMembers>

可使用版本: 2.8

在一个联合类型中剔除成员变量

type keys = 'test1' | 'test2' | 'test3';

type Type = Exclude<keys, 'test3'>; // type Type = "test1" | "test2"

Extract<unionType, Union>

可使用版本: 2.8

在一个联合类型中返回指定 key 的联合类型

type extractKey = 'test1' | 'test2';

type Type = 'test1' | 'test2' | 'test3';

type newType = Extract<Type, extractKey>; // type newType = "test1" | "test2"

NonNullable<Type>

可使用版本: 2.8

剔除nullundefined类型

  type keys = string | null | number | undefined

  type Type = NonNullable // type Type = string | number

Parameters<Type>

可使用版本: 3.1

返回函数中的参数的类型 (返回元组类型)

type func = (test1: string, test2: number) => void;

type Param = Parameters<func>; // type Param = [test1: string, test2: number]

ConstructorParameters<Type>

可使用版本: 3.1

返回类构造函数中的类型 (返回元组类型)

interface testClass {
  new (test1: string, test2: number): void;
}

type constructorType = ConstructorParameters<testClass>;
// type constructorType = [test1: string,test2: number]

ReturnType<Type>

可使用版本: 2.8

返回一个函数的返回类型

type func = () => { test1: string; test2: string };

type funcType = ReturnType<func>; // type funcType = { test1: string, test2: string }

InstanceType<Type>

可使用版本: 2.8

返回一个类的构造函数的返回值类型

interface testClass {
  new (): { test1: string; test2: number };
}

type Type = InstanceType<testClass>; // type Type = { test1: string, test2: string }

ThisParameterType<Type>

可使用版本: 3.3

返回函数参数中this参数的类型

type func = (this: { test1: string; test2: number }) => void;

type Type = ThisParameterType<func>; // type Type = { test1: string, test2: number }

OmitThisParameter<Type>

可使用版本: 3.3

去掉函数参数中的this参数,返回去掉this参数后的函数类型

type func = (this: { test1: string; test2: number }, test: string) => void;

type Type = OmitThisParameter<func>; // type Type = (test: string) => void

ThisType<Type>

可使用版本: 2.3

这个工具函数不会返回类型,它用作上下文类型标记。函数没有返回值时才可以使用该工具

type ObjectDescriptor<D, M> = {
  data?: D;
  methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
};

function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
  let data: object = desc.data || {};
  let methods: object = desc.methods || {};
  return { ...data, ...methods } as D & M;
}

let obj = makeObject({
  data: { x: 0, y: 0 },
  methods: {
    moveBy(dx: number, dy: number) {
      // x y 会被自动推到出来
      this.x += dx; // Strongly typed this
      this.y += dy; // Strongly typed this
    },
  },
});

字符串操作类型

可使用版本: 4.1

Uppercase<StringType> 转大写

type str = 'hello';

type UpperStr = Uppercase<str>; // type UpperStr = "HELLO"

Lowercase<StringType> 转小写

type str = 'HELLO';

type LowerStr = Lowercase<str>; // type UpperStr = "hello"

Capitalize<StringType> 首字母大写

type str = 'hello';

type CapitalizeStr = Capitalize<str>; // type CapitalizeStr = "Hello"

Uncapitalize<StringType> 首字母小写

type str = 'HelloWorld';

type UncapitalizeStr = Uncapitalize<str>; // type UncapitalizeStr = "helloWorld"