TypeScript:UtilityTypes
lastmod 2024-01-27created 2023-03-0210 edits
TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.
Awaited<Type>
Partial<Type>
Constructs a type with all properties of Type set to optional. This utility returns a type that represents all subsets of a given type.
Required<Type>
Constructs a type consisting of all properties of Type set to required.
Readonly<Type>
Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.
Record
Constructs a type with a set of properties Keys of type Type. This utility can be used to map the properties of a type to another type.
Pick
Constructs a type by picking the set of properties Keys from Type.
Omit
Constructs a type by picking all properties from Type and then removing Keys.
Exclude
Constructs a type by excluding from UnionType all properties that are assignable to ExcludedMembers.
Extract
Constructs a type by extracting from Type all properties that are assignable to Union.
NonNullable<Type>
Constructs a type by excluding null and undefined from Type.
Parameters<Type>
Constructs a tuple type from the types of the parameters of a function type Type.
ConstructorParameters<Type>
Lets you extract all parameter types of a constructor function type. It produces a tuple type with all the parameter types (or never if Type is not a function).
ReturnType<Type>
Constructs a type consisting of the return type of function Type.
InstanceType<Type>
Constructs a type consisting of the instance type of a constructor function type Type.
ThisParameterType<Type>
Extracts the type of the this parameter of a function type, or unknown if the function type has no this parameter.
Note: This type only works correctly when --strictFunctionTypes is enabled. See #32964.
OmitThisParameter<Type>
Removes the 'this' parameter from a function type.
Note: This type only works correctly when --strictFunctionTypes is enabled. See #32964.
ThisType<Type>
This utility does not return a transformed type. Instead, it serves as a marker for a contextual this type. Note that the --noImplicitThis flag must be used to use this utility.
Intrinsic String Manipulation Types
Uppercase<StringType>
Lowercase<StringType>
Capitalize<StringType>
Uncapitalize<StringType>
How to get the full list of keys from a specific instance
export const appPaths = {
login: '/login',
loginPkceError: '/login/pkce/error',
passwordResetUpdate: '/password/reset/update',
};
const propsArray = Object.keys(appPaths) as Array<keyof typeof appPaths>;
console.debug(propsArray);
How to turn a string array into string literals
and
are equivalent.
Changing interface property types based on a string literal
interface PreferenceFieldCommon {
key: string;
label?: string;
detail?: string;
}
type PreferenceFieldSpecialize =
| {
type: 'text';
}
| {
type: 'select';
options?: Array<SelectOptions>;
}
| {
type: 'radio';
options?: Array<RadioOptions>;
};
export type PreferenceField = PreferenceFieldCommon & PreferenceFieldSpecialize;
Analysis
- Digging into Typescript utility classes. Focusing on the Utility Types Docs | by Harry The Great | Harry's Nomad Coding | Medium
- In TypeScript, what do "extends keyof" and "in keyof" mean? - Stack Overflow
keyof keyword
The keyof keyword returns the keys of all properties present in a type as a union.
never type
never is a type that ignores errors without halting the process when they occur. It can be used as the primitive type of every other type except any. Although it is a type, you cannot actually declare a variable with the never type; it is mainly used as a function's return type, where it ignores errors when they occur and lets execution continue.
See also
Favorite site
- TypeScript: Documentation - Utility Types
- typescript - Utility types (Partial, Omit, Pick) | Record rather than memory
- How to use Typescript Partial, Required, Pick - Fall in IT.
- Utility Types · GitBook