TypeScript provides several utility types to facilitate common type transformations.
ConstructorParameters<Type>
type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
Constructs a tuple or array type from the types of a constructor function type.
It produces a tuple type with all the parameter types (or the type never if Type is not a function).
Exclude<Type, ExcludedUnion>
type Exclude<T, U> = T extends U ? never : T;
Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.
Extract<Type, Union>
type Extract<T, U> = T extends U ? T : never;
Constructs a type by extracting from Type all union members that are assignable to Union.
InstanceType<Type>
type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;
Constructs a type consisting of the instance type of a constructor function in Type.
NonNullable<Type>
type NonNullable<T> = T extends null | undefined ? never : T;
Constructs a type by excluding null and undefined from Type.
Omit<Type, Keys>
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
Constructs a type by picking all properties from Type and then removing Keys.
OmitThisParameter<Type>
type OmitThisParameter<T> = unknown extends ThisParameterType<T> ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T;
Removes the this parameter from Type.
Parameters<Type>
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
Constructs a tuple type from the types used in the parameters of a function type Type.
Partial<Type>
type Partial<T> = {
[P in keyof T]?: T[P];
};
Constructs a type with all properties of Type set to optional.
interface IToDo {
content: string;
complete: boolean;
}
type OptionalToDo = Partial<IToDo>;
let toDo: OptionalToDo = {
content: "Blogging",
};
The usage of all other types is the same as this.
Pick<Type, Keys>
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
Constructs a type by picking the set of properties Keys from Type.
Readonly<Type>
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
Constructs a type with all properties of Type set to readonly.
Record<Keys, Type>
type Record<K extends keyof any, T> = {
[P in K]: T;
};
Constructs an object type whose property keys are Keys and whose property values are Type.
Required<Type>
type Required<T> = {
[P in keyof T]-?: T[P];
};
Constructs a type consisting of all properties of Type set to required.
ReturnType<Type>
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
Constructs a type consisting of the return type of function Type.
ThisParameterType<Type>
type ThisParameterType<T> = T extends (this: infer U, ...args: any[]) => any ? U : unknown;
Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.
ThisType<Type>
interface ThisType<T> { }
This utility does not return a transformed type. Instead, it serves as a marker for a contextual this type.
'Web > JavaScript' 카테고리의 다른 글
[Vue3] Getting Started with Vue3 + TypeScript + TailwindCSS (0) | 2021.06.02 |
---|---|
[TypeScript] Template Literal Types (0) | 2021.03.21 |
[Jest] Testing with Error Return (0) | 2020.12.28 |
[React] Get Keyboard Input (TypeScript) (0) | 2020.12.28 |
[TypeScript] Type Inference (0) | 2020.10.25 |
댓글