TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.
Awaited<Type>
Released: 4.5
This type is meant to model operations like await in async functions, or the
.then() method on Promises - specifically, the way that they recursively
unwrap Promises.
Example
tsTrytype = <<string>>;type A = stringtype = <<<number>>>;type B = numbertype = <boolean | <number>>;type C = number | boolean
Partial<Type>
Released:
2.1
Constructs a type with all properties of Type set to optional. This utility will return a type that represents all subsets of a given type.
Example
tsTryinterface {: string;: string;}function(: , : <>) {return { ..., ... };}const = {:"organize desk",:"clear clutter",};const = (, {:"throw out trash",});
Required<Type>
Released:
2.8
Constructs a type consisting of all properties of Type set to required. The opposite of Partial.
Example
tsTryinterface {?: number;?: string;}const: = { :5 };const: <> = { :5 };Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.2741Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.
Readonly<Type>
Released:
2.1
Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.
Example
tsTryinterface {: string;}const: <> = {:"Delete inactive users",};. = "Hello";Cannot assign to 'title' because it is a read-only property.2540Cannot assign to 'title' because it is a read-only property.
This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a frozen object).
Object.freeze
tsfunctionfreeze<Type>(obj: Type): Readonly<Type>;
Record<Keys, Type>
Released:
2.1
Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type.
Example
tsTrytype = "miffy" | "boris" | "mordred";interface {: number;: string;}const: <, > = {: { :10, :"Persian" },: { :5, :"Maine Coon" },: { :16, :"British Shorthair" },};.;const cats: Record<CatName, CatInfo>
Pick<Type, Keys>
Released:
2.1
Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type.
Example
tsTryinterface {: string;: string;: boolean;}type = <, "title" | "completed">;const: = {:"Clean room",:false,};;const todo: TodoPreview
Omit<Type, Keys>
Released:
3.5
Constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals). The opposite of Pick.
Example
tsTryinterface {: string;: string;: boolean;: number;}type = <, "description">;const: = {:"Clean room",:false,:1615544252770,};;const todo: TodoPreviewtype = <, "completed" | "createdAt">;const: = {:"Pick up kids",:"Kindergarten closes at 5pm",};;const todoInfo: TodoInfo
Exclude<UnionType, ExcludedMembers>
Released:
2.8
Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers.
Example
tsTrytype = <"a" | "b" | "c", "a">;type T0 = "b" | "c"type = <"a" | "b" | "c", "a" | "b">;type T1 = "c"type = <string | number | (() =>void), >;type T2 = string | numbertype =| { : "circle"; : number }| { : "square"; : number }| { : "triangle"; : number; : number };type = <, { : "circle" }>type T3 = { kind: "square"; x: number; } | { kind: "triangle"; x: number; y: number; }
Extract<Type, Union>
Released:
2.8
Constructs a type by extracting from Type all union members that are assignable to Union.
Example
tsTrytype = <"a" | "b" | "c", "a" | "f">;type T0 = "a"type = <string | number | (() =>void), >;type T1 = () => voidtype =| { : "circle"; : number }| { : "square"; : number }| { : "triangle"; : number; : number };type = <, { : "circle" }>type T2 = { kind: "circle"; radius: number; }
NonNullable<Type>
Released:
2.8
Constructs a type by excluding null and undefined from Type.
Example
tsTrytype = <string | number | undefined>;type T0 = string | numbertype = <string[] | null | undefined>;type T1 = string[]
Parameters<Type>
Released:
3.1
Constructs a tuple type from the types used in the parameters of a function type Type.
For overloaded functions, this will be the parameters of the last signature; see Inferring Within Conditional Types.
Example
tsTrydeclarefunction(: { : number; : string }): void;type = <() =>string>;type T0 = []type = <(: string) =>void>;type T1 = [s: string]type = <<>(: ) =>>;type T2 = [arg: unknown]type = <typeof>;type T3 = [arg: { a: number; b: string; }]type = <any>;type T4 = unknown[]type = <never>;type T5 = nevertype = <>;Type 'string' does not satisfy the constraint '(...args: any) => any'.2344Type 'string' does not satisfy the constraint '(...args: any) => any'.type T6 = nevertype = <>;Type 'Function' does not satisfy the constraint '(...args: any) => any'. Type 'Function' provides no match for the signature '(...args: any): any'.2344Type 'Function' does not satisfy the constraint '(...args: any) => any'. Type 'Function' provides no match for the signature '(...args: any): any'.type T7 = never
ConstructorParameters<Type>
Released:
3.1
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).
Example
tsTrytype = <>;type T0 = [message?: string]type = <>;type T1 = string[]type = <>;type T2 = [pattern: string | RegExp, flags?: string]class {constructor(: number, : string) {}}type = <typeof>;type T3 = [a: number, b: string]type = <any>;type T4 = unknown[]type = <>;Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'. Type 'Function' provides no match for the signature 'new (...args: any): any'.2344Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'. Type 'Function' provides no match for the signature 'new (...args: any): any'.type T5 = never
ReturnType<Type>
Released:
2.8
Constructs a type consisting of the return type of function Type.
For overloaded functions, this will be the return type of the last signature; see Inferring Within Conditional Types.
Example
tsTrydeclarefunction(): { : number; : string };type = <() =>string>;type T0 = stringtype = <(: string) =>void>;type T1 = voidtype = <<>() =>>;type T2 = unknowntype = <<extends, extendsnumber[]>() =>>;type T3 = number[]type = <typeof>;type T4 = { a: number; b: string; }type = <any>;type T5 = anytype = <never>;type T6 = nevertype = <>;Type 'string' does not satisfy the constraint '(...args: any) => any'.2344Type 'string' does not satisfy the constraint '(...args: any) => any'.type T7 = anytype = <>;Type 'Function' does not satisfy the constraint '(...args: any) => any'. Type 'Function' provides no match for the signature '(...args: any): any'.2344Type 'Function' does not satisfy the constraint '(...args: any) => any'. Type 'Function' provides no match for the signature '(...args: any): any'.type T8 = any
InstanceType<Type>
Released:
2.8
Constructs a type consisting of the instance type of a constructor function in Type.
Example
tsTryclass {= 0;= 0;}type = <typeof>;type T0 = Ctype = <any>;type T1 = anytype = <never>;type T2 = nevertype = <>;Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.2344Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.type T3 = anytype = <>;Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'. Type 'Function' provides no match for the signature 'new (...args: any): any'.2344Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'. Type 'Function' provides no match for the signature 'new (...args: any): any'.type T4 = any
NoInfer<Type>
Released:
5.4
Blocks inferences to the contained type. Other than blocking inferences, NoInfer<Type> is
identical to Type.
Example
tsfunctioncreateStreetLight<Cextendsstring>(colors: C[],defaultColor?: NoInfer<C>,) {// ...}createStreetLight(["red", "yellow", "green"], "red"); // OKcreateStreetLight(["red", "yellow", "green"], "blue"); // Error
ThisParameterType<Type>
Released:
3.3
Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.
Example
tsTryfunction(: ) {returnthis.(16);}function(: <typeof>) {return.();}
OmitThisParameter<Type>
Released:
3.3
Removes the this parameter from Type. If Type has no explicitly declared this parameter, the result is simply Type. Otherwise, a new function type with no this parameter is created from Type. Generics are erased and only the last overload signature is propagated into the new function type.
Example
tsTryfunction(: ) {returnthis.(16);}const: <typeof> = .(5);.(());
ThisType<Type>
Released:
2.3
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 enabled to use this utility.
Example
tsTrytype<, > = {?: ;?: & < & >; // Type of 'this' in methods is D & M};function<, >(: <, >): & {let: object = . || {};let: object = . || {};return { ..., ... } as & ;}let = ({: { :0, :0 },: {(: number, : number) {this. += ; // Strongly typed thisthis. += ; // Strongly typed this},},});. = 10;. = 20;.(5, 5);
In the example above, the methods object in the argument to makeObject has a contextual type that includes ThisType<D & M> and therefore the type of this in methods within the methods object is { x: number, y: number } & { moveBy(dx: number, dy: number): void }. Notice how the type of the methods property simultaneously is an inference target and a source for the this type in methods.
The ThisType<T> marker interface is simply an empty interface declared in lib.d.ts. Beyond being recognized in the contextual type of an object literal, the interface acts like any empty interface.
Intrinsic String Manipulation Types
Uppercase<StringType>
Lowercase<StringType>
Capitalize<StringType>
Uncapitalize<StringType>
To help with string manipulation around template string literals, TypeScript includes a set of types which can be used in string manipulation within the type system. You can find those in the Template Literal Types documentation.
