Introduction

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

Table of contents

Partial<T>

Constructs a type with all properties of T 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. });

Readonly<T>

Constructs a type with all properties of T 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'; // Error: cannot reassign a readonly 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
  1. function freeze<T>(obj: T): Readonly<T>;

Record<K,T>

Constructs a type with a set of properties K of type T. 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 x: Record<Page, PageInfo> = {
  6. about: { title: 'about' },
  7. contact: { title: 'contact' },
  8. home: { title: 'home' },
  9. };

Pick<T,K>

Constructs a type by picking the set of properties K from T.

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. };

Omit<T,K>

Constructs a type by picking all properties from T and then removing K.

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. };

Exclude<T,U>

Constructs a type by excluding from T all properties that are assignable to U.

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

Extract<T,U>

Constructs a type by extracting from T all properties that are assignable to U.

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

NonNullable<T>

Constructs a type by excluding null and undefined from T.

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

ReturnType<T>

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

Example
  1. type T0 = ReturnType<() => string>; // string
  2. type T1 = ReturnType<(s: string) => void>; // void
  3. type T2 = ReturnType<(<T>() => T)>; // {}
  4. type T3 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]
  5. type T4 = ReturnType<typeof f1>; // { a: number, b: string }
  6. type T5 = ReturnType<any>; // any
  7. type T6 = ReturnType<never>; // any
  8. type T7 = ReturnType<string>; // Error
  9. type T8 = ReturnType<Function>; // Error

InstanceType<T>

Constructs a type consisting of the instance type of a constructor function type T.

Example
  1. class C {
  2. x = 0;
  3. y = 0;
  4. }
  5. type T0 = InstanceType<typeof C>; // C
  6. type T1 = InstanceType<any>; // any
  7. type T2 = InstanceType<never>; // any
  8. type T3 = InstanceType<string>; // Error
  9. type T4 = InstanceType<Function>; // Error

Required<T>

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

Example
  1. interface Props {
  2. a?: number;
  3. b?: string;
  4. };
  5. const obj: Props = { a: 5 }; // OK
  6. const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing

ThisType<T>

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