TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.

Partial<Type>

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
  1. interface Todo {
  2. title: string;
  3. description: string;
  4. }
  5. function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) {
  6. return { ...todo, ...fieldsToUpdate };
  7. }
  8. const todo1 = {
  9. title: "organize desk",
  10. description: "clear clutter",
  11. };
  12. const todo2 = updateTodo(todo1, {
  13. description: "throw out trash",
  14. });Try

Readonly<Type>

Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.

Example
  1. interface Todo {
  2. title: string;
  3. }
  4. const todo: Readonly<Todo> = {
  5. title: "Delete inactive users",
  6. };
  7. todo.title = "Hello";
  8. Cannot assign to 'title' because it is a read-only property.2540Cannot assign to 'title' because it is a read-only property.Try

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
  1. function freeze<Type>(obj: Type): Readonly<Type>;

Record<Keys,Type>

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.

Example
  1. interface PageInfo {
  2. title: string;
  3. }
  4. type Page = "home" | "about" | "contact";
  5. const nav: Record<Page, PageInfo> = {
  6. about: { title: "about" },
  7. contact: { title: "contact" },
  8. home: { title: "home" },
  9. };
  10. nav.about;
  11. // ^ = Could not get LSP result: bou>t<;
  12. /Try

Pick<Type, Keys>

Constructs a type by picking the set of properties Keys from Type.

Example
  1. interface Todo {
  2. title: string;
  3. description: string;
  4. completed: boolean;
  5. }
  6. type TodoPreview = Pick<Todo, "title" | "completed">;
  7. const todo: TodoPreview = {
  8. title: "Clean room",
  9. completed: false,
  10. };
  11. todo;
  12. // ^ = const todo: PickTry

Omit<Type, Keys>

Constructs a type by picking all properties from Type and then removing Keys.

Example
  1. interface Todo {
  2. title: string;
  3. description: string;
  4. completed: boolean;
  5. }
  6. type TodoPreview = Omit<Todo, "description">;
  7. const todo: TodoPreview = {
  8. title: "Clean room",
  9. completed: false,
  10. };
  11. todo;
  12. // ^ = const todo: PickTry

Exclude<Type, ExcludedUnion>

Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.

Example
  1. type T0 = Exclude<"a" | "b" | "c", "a">;
  2. // ^ = type T0 = "b" | "c"
  3. type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
  4. // ^ = type T1 = "c"
  5. type T2 = Exclude<string | number | (() => void), Function>;
  6. // ^ = type T2 = string | numberTry

Extract<Type, Union>

Constructs a type by extracting from Type all union members that are assignable to Union.

Example
  1. type T0 = Extract<"a" | "b" | "c", "a" | "f">;
  2. // ^ = type T0 = "a"
  3. type T1 = Extract<string | number | (() => void), Function>;
  4. // ^ = type T1 = () => voidTry

NonNullable<Type>

Constructs a type by excluding null and undefined from Type.

Example
  1. type T0 = NonNullable<string | number | undefined>;
  2. // ^ = type T0 = string | number
  3. type T1 = NonNullable<string[] | null | undefined>;
  4. // ^ = type T1 = string[]Try

Parameters<Type>

Constructs a tuple type from the types used in the parameters of a function type Type.

Example
  1. declare function f1(arg: { a: number; b: string }): void;
  2. type T0 = Parameters<() => string>;
  3. // ^ = type T0 = []
  4. type T1 = Parameters<(s: string) => void>;
  5. // ^ = type T1 = [s: string]
  6. type T2 = Parameters<<T>(arg: T) => T>;
  7. // ^ = type T2 = [arg: unknown]
  8. type T3 = Parameters<typeof f1>;
  9. // ^ = type T3 = [arg: {
  10. a: number;
  11. b: string;
  12. }]
  13. type T4 = Parameters<any>;
  14. // ^ = type T4 = unknown[]
  15. type T5 = Parameters<never>;
  16. // ^ = type T5 = never
  17. type T6 = Parameters<string>;
  18. Type 'string' does not satisfy the constraint '(...args: any) => any'.2344Type 'string' does not satisfy the constraint '(...args: any) => any'.// ^ = type T6 = never
  19. type T7 = Parameters<Function>;
  20. Type 'Function' does not satisfy the constraint '(...args: any) => any'.
  21. Type 'Function' provides no match for the signature '(...args: any): any'.2344Type 'Function' does not satisfy the constraint '(...args: any) => any'.
  22. Type 'Function' provides no match for the signature '(...args: any): any'.// ^ = type T7 = neverTry

ConstructorParameters<Type>

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
  1. type T0 = ConstructorParameters<ErrorConstructor>;
  2. // ^ = type T0 = [message?: string]
  3. type T1 = ConstructorParameters<FunctionConstructor>;
  4. // ^ = type T1 = string[]
  5. type T2 = ConstructorParameters<RegExpConstructor>;
  6. // ^ = type T2 = [pattern: string | RegExp, flags?: string]
  7. type T3 = ConstructorParameters<any>;
  8. // ^ = type T3 = unknown[]
  9. type T4 = ConstructorParameters<Function>;
  10. Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
  11. Type 'Function' provides no match for the signature 'new (...args: any): any'.2344Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
  12. Type 'Function' provides no match for the signature 'new (...args: any): any'.// ^ = type T4 = neverTry

ReturnType<Type>

Constructs a type consisting of the return type of function Type.

Example
  1. declare function f1(): { a: number; b: string };
  2. type T0 = ReturnType<() => string>;
  3. // ^ = type T0 = string
  4. type T1 = ReturnType<(s: string) => void>;
  5. // ^ = type T1 = void
  6. type T2 = ReturnType<<T>() => T>;
  7. // ^ = type T2 = unknown
  8. type T3 = ReturnType<<T extends U, U extends number[]>() => T>;
  9. // ^ = type T3 = number[]
  10. type T4 = ReturnType<typeof f1>;
  11. // ^ = type T4 = {
  12. a: number;
  13. b: string;
  14. }
  15. type T5 = ReturnType<any>;
  16. // ^ = type T5 = any
  17. type T6 = ReturnType<never>;
  18. // ^ = type T6 = never
  19. type T7 = ReturnType<string>;
  20. Type 'string' does not satisfy the constraint '(...args: any) => any'.2344Type 'string' does not satisfy the constraint '(...args: any) => any'.// ^ = type T7 = any
  21. type T8 = ReturnType<Function>;
  22. Type 'Function' does not satisfy the constraint '(...args: any) => any'.
  23. Type 'Function' provides no match for the signature '(...args: any): any'.2344Type 'Function' does not satisfy the constraint '(...args: any) => any'.
  24. Type 'Function' provides no match for the signature '(...args: any): any'.// ^ = type T8 = anyTry

InstanceType<Type>

Constructs a type consisting of the instance type of a constructor function in Type.

Example
  1. class C {
  2. x = 0;
  3. y = 0;
  4. }
  5. type T0 = InstanceType<typeof C>;
  6. // ^ = type T0 = C
  7. type T1 = InstanceType<any>;
  8. // ^ = type T1 = any
  9. type T2 = InstanceType<never>;
  10. // ^ = type T2 = never
  11. type T3 = InstanceType<string>;
  12. Type 'string' does not satisfy the constraint 'new (...args: any) => any'.2344Type 'string' does not satisfy the constraint 'new (...args: any) => any'.// ^ = type T3 = any
  13. type T4 = InstanceType<Function>;
  14. Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
  15. Type 'Function' provides no match for the signature 'new (...args: any): any'.2344Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
  16. Type 'Function' provides no match for the signature 'new (...args: any): any'.// ^ = type T4 = anyTry

Required<Type>

Constructs a type consisting of all properties of T set to required. The opposite of Partial.

Example
  1. interface Props {
  2. a?: number;
  3. b?: string;
  4. }
  5. const obj: Props = { a: 5 };
  6. const obj2: Required<Props> = { a: 5 };
  7. 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>'.Try

ThisParameterType<Type>

Extracts the type of the this parameter for a function type, or unknown if the function type has no this parameter.

Example
  1. function toHex(this: Number) {
  2. return this.toString(16);
  3. }
  4. function numberToString(n: ThisParameterType<typeof toHex>) {
  5. return toHex.apply(n);
  6. }Try

OmitThisParameter<Type>

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
  1. function toHex(this: Number) {
  2. return this.toString(16);
  3. }
  4. const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5);
  5. console.log(fiveToHex());Try

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 enabled to use this utility.

Example
  1. type ObjectDescriptor<D, M> = {
  2. data?: D;
  3. methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
  4. };
  5. function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
  6. let data: object = desc.data || {};
  7. let methods: object = desc.methods || {};
  8. return { ...data, ...methods } as D & M;
  9. }
  10. let obj = makeObject({
  11. data: { x: 0, y: 0 },
  12. methods: {
  13. moveBy(dx: number, dy: number) {
  14. this.x += dx; // Strongly typed this
  15. this.y += dy; // Strongly typed this
  16. },
  17. },
  18. });
  19. obj.x = 10;
  20. obj.y = 20;
  21. obj.moveBy(5, 5);Try

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): number }. 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.