TypeScript 提供了一些实用的公共使用类型,这些工具类型是放在全局中的
Partial<Type>
可使用版本: 2.1
这个工具会返回一个指定类型的子集
type Type1 = {
test1: string;
test2: string;
};
type Type2 = Partial<Type1>;
Required<Type>
可使用版本: 2.8
使指定类型的所有属性变为必须实现
type Type1 = {
test1?: string;
test2?: string;
};
type Type2 = Requires<Type1>;
Readonly<Type>
可使用版本: 2.1
将指定类型变为只读
type Type1 = {
test1: string;
test2: string;
};
type Type2 = Readonly<Type1>;
Record<keys, Type>
可使用版本: 2.1
生成一个值类型为Type对象类型
type keys = 'test1' | 'test2';
type Obj = Record<keys, string>;
Pick<Type, keys>
可使用版本: 2.1
在指定的类型中选择指定的 key,并返回一个新对象
type keys = 'test1' | 'test2';
type Obj = {
test1: string;
test2: string;
test3: string;
};
type newObj = Pick<Obj, keys>;
Omit<Type, Keys>
可使用版本: 3.5
在一个指定类型中删除指定的 key,并返回一个新对象
type keys = 'test3';
type Obj = {
test1: string;
test2: string;
test3: string;
};
type newObj = Omit<Obj, keys>;
Exclude<UnionType, ExcludeMembers>
可使用版本: 2.8
在一个联合类型中剔除成员变量
type keys = 'test1' | 'test2' | 'test3';
type Type = Exclude<keys, 'test3'>;
可使用版本: 2.8
在一个联合类型中返回指定 key 的联合类型
type extractKey = 'test1' | 'test2';
type Type = 'test1' | 'test2' | 'test3';
type newType = Extract<Type, extractKey>;
NonNullable<Type>
可使用版本: 2.8
剔除null或undefined类型
type keys = string | null | number | undefined
type Type = NonNullable<keys> // type Type = string | number
Parameters<Type>
可使用版本: 3.1
返回函数中的参数的类型 (返回元组类型)
type func = (test1: string, test2: number) => void;
type Param = Parameters<func>;
ConstructorParameters<Type>
可使用版本: 3.1
返回类构造函数中的类型 (返回元组类型)
interface testClass {
new (test1: string, test2: number): void;
}
type constructorType = ConstructorParameters<testClass>;
ReturnType<Type>
可使用版本: 2.8
返回一个函数的返回类型
type func = () => { test1: string; test2: string };
type funcType = ReturnType<func>;
InstanceType<Type>
可使用版本: 2.8
返回一个类的构造函数的返回值类型
interface testClass {
new (): { test1: string; test2: number };
}
type Type = InstanceType<testClass>;
ThisParameterType<Type>
可使用版本: 3.3
返回函数参数中this参数的类型
type func = (this: { test1: string; test2: number }) => void;
type Type = ThisParameterType<func>;
OmitThisParameter<Type>
可使用版本: 3.3
去掉函数参数中的this参数,返回去掉this参数后的函数类型
type func = (this: { test1: string; test2: number }, test: string) => void;
type Type = OmitThisParameter<func>;
ThisType<Type>
可使用版本: 2.3
这个工具函数不会返回类型,它用作上下文类型标记。函数没有返回值时才可以使用该工具
type ObjectDescriptor<D, M> = {
data?: D;
methods?: M & ThisType<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) {
this.x += dx;
this.y += dy;
},
},
});
字符串操作类型
可使用版本: 4.1
Uppercase<StringType> 转大写
type str = 'hello';
type UpperStr = Uppercase<str>;
Lowercase<StringType> 转小写
type str = 'HELLO';
type LowerStr = Lowercase<str>;
Capitalize<StringType> 首字母大写
type str = 'hello';
type CapitalizeStr = Capitalize<str>;
Uncapitalize<StringType> 首字母小写
type str = 'HelloWorld';
type UncapitalizeStr = Uncapitalize<str>;