Utility Types

Introduction

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

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 ).

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

Pick<T,K>

Example

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. completed: false,
  9. };

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

ReturnType<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 T6 = ReturnType<never>; // any
  7. type T7 = ReturnType<string>; // Error
  8. 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

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.