In the process of using TypeScript, we are type-oriented programming. In order to meet different work scenarios, we need to transform known types.
For the convenience of TypeScript users, the TypeScript development team has provided us with a number of useful built-in utility types.
With these utility types, we can easily convert types, extract types, exclude types, or get the parameter types or return value types of functions.
In this article, I have selected 15 very useful types from TypeScript's built-in utility types, and introduced their usage and inner working principle in the form of images. After reading this article, I believe you can really master these built-in types. Usage of utility types.
1.Partial<Type>
Constructs a type where all properties of Type are set to optional.
/**
* Make all properties in T optional.
* typescript/lib/lib.es5.d.ts
*/
type Partial<T> = {
[P in keyof T]?: T[P];
};
2.Required<Type>
Constructs a type consisting of all properties set to required Type, the opposite of part.
/**
* Make all properties in T required.
* typescript/lib/lib.es5.d.ts
*/
type Required<T> = {
[P in keyof T]-?: T[P];
};
3.Readonly<Type>
Construct a Type with all properties set to readonly, which means that the properties of the constructed type cannot be reassigned.
/**
* Make all properties in T readonly.
* typescript/lib/lib.es5.d.ts
*/
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
4.Record<Keys, Type>
Constructs an object type whose property keys are Keys and whose property values are Type, this utility can be used to map properties of one type to another type.
/**
* Construct a type with a set of properties K of type T.
* typescript/lib/lib.es5.d.ts
*/
type Record<K extends keyof any, T> = {
[P in K]: T;
};
5.Exclude<UnionType, ExcludedMembers>
Constructs a type by excluding from UnionType all union members assignable to ExcludedMembers.
/**
* Exclude from T those types that are assignable to U.
* typescript/lib/lib.es5.d.ts
*/
type Exclude<T, U> = T extends U ? never : T;
6.Extract<Type, Union>
Constructs a type by extracting from Type all union members assignable to Union.
/**
* Extract from T those types that are assignable to U.
* typescript/lib/lib.es5.d.ts
*/
type Extract<T, U> = T extends U ? T : never;
7.Pick<Type, Keys>
Constructs a type by selecting a set of property Keys (string literals or unions of string literals) from Type.
/**
* From T, pick a set of properties whose keys are in the union K.
* typescript/lib/lib.es5.d.ts
*/
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
8.Omit<Type, Keys>
Constructs a type by selecting all properties from Type and then removing Keys (string literals or unions of string literals).
/**
* Construct a type with the properties of T except for those
* in type K.
* typescript/lib/lib.es5.d.ts
*/
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
9.NonNullable<Type>
Constructs a type by excluding null and undefined from Type.
/**
* Exclude null and undefined from T.
* typescript/lib/lib.es5.d.ts
*/
type NonNullable<T> = T extends null | undefined ? never : T;
10.Parameters<Type>
Constructs a tuple type from the types used in the arguments of the function type Type.
/**
* Obtain the parameters of a function type in a tuple.
* typescript/lib/lib.es5.d.ts
*/
type Parameters<T extends (...args: any) => any> = T extends
(...args: infer P) => any ? P : never;
11.ReturnType<Type>
Constructs a type consisting of the return type of the function Type.
/**
* Obtain the return type of a function type.
* typescript/lib/lib.es5.d.ts
*/
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
12.Uppercase<StringType>
Converts a string literal type to uppercase.
13. Lowercase <StringType>
Converts a string literal type to lowercase.
14. Uppercase <StringType>
Converts the first character of a string literal type to uppercase.
15. Cancel capitalization <StringType>
Converts the first character of a string literal type to lowercase.
In addition to these utility types above, there are some other commonly used TypeScript built-in utility types, as follows:
-
ConstructorParameters: Constructs a tuple or array type based on the type of the constructor type. It yields a tuple type containing the types of all arguments (or never if Type is not a function).
-
InstanceType: Constructs a type consisting of instance types of constructors in Type.
-
ThisParameterType: Extracts the type of this parameter for the function type, or unknown if the function type does not have this parameter.