/** * Exclude from T those types that are assignable to U. * typescript/lib/lib.es5.d.ts */typeExclude<T, U>=TextendsU?never : T;
1.
2.
3.
4.
5.
6. Extract<Type, Union>
通过从 Type 中提取可分配给 Union 的所有联合成员来构造一个类型。
/** * Extract from T those types that are assignable to U. * typescript/lib/lib.es5.d.ts */typeExtract<T, U>=TextendsU?T : never;
1.
2.
3.
4.
5.
7. Pick<Type, Keys>
通过从 Type 中选择一组属性键(字符串文字或字符串文字的并集)来构造一个类型。
/** * From T, pick a set of properties whose keys are in the union K. * typescript/lib/lib.es5.d.ts */typePick<T, KextendskeyofT>= {
[PinK]: T[P];
};
1.
2.
3.
4.
5.
6.
7.
8. Omit<Type, Keys>
通过从 Type 中选择所有属性然后删除键(字符串文字或字符串文字的并集)来构造一个类型。
/** * Construct a type with the properties of T except for those * in type K. * typescript/lib/lib.es5.d.ts */typeOmit<T, Kextendskeyofany>=Pick<T, Exclude<keyofT, K>>;
1.
2.
3.
4.
5.
6.
9. NonNullable<Type>
通过从 Type 中排除 null 和 undefined 来构造一个类型。
/** * Exclude null and undefined from T. * typescript/lib/lib.es5.d.ts */typeNonNullable<T>=Textendsnull|undefined?never : T;
1.
2.
3.
4.
5.
10. Parameters<Type>
根据函数类型 Type 的参数中使用的类型构造元组类型。
/** * Obtain the parameters of a function type in a tuple. * typescript/lib/lib.es5.d.ts */typeParameters<Textends (...args: any) =>any>=Textends
(...args: inferP) =>any?P : never;
1.
2.
3.
4.
5.
6.
11. ReturnType<Type>
构造一个由函数 Type 的返回类型组成的类型。
/** * Obtain the return type of a function type. * typescript/lib/lib.es5.d.ts */typeReturnType<Textends (...args: any) =>any>=Textends (...args: any) =>inferR?R : any;
1.
2.
3.
4.
5.
12. Uppercase<StringType>
将字符串文字类型转换为大写。
13. Lowercase<StringType>
将字符串文字类型转换为小写。
14. Capitalize<StringType>
将字符串文字类型的第一个字符转换为大写。
15. Uncapitalize<StringType>
将字符串文字类型的第一个字符转换为小写。
除了上面介绍的这些实用类型外,其他常用的 TypeScript 内置实用类型如下:
ConstructorParameters<Type>:根据构造函数类型的类型构造元组或数组类型。它生成一个包含所有参数类型的元组类型(如果 Type 不是函数,则生成 never 类型)。