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[]

Parameters<T>

Constructs a tuple type of the types of the parameters of a function type T.

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

ConstructorParameters<T>

The ConstructorParameters<T> type lets us extract all parameter types of a constructor function type. It produces a tuple type with all the parameter types (or the type never if T is not a function).

Example
  1. type T0 = ConstructorParameters<ErrorConstructor>; // [(string | undefined)?]
  2. type T1 = ConstructorParameters<FunctionConstructor>; // string[]
  3. type T2 = ConstructorParameters<RegExpConstructor>; // [string, (string | undefined)?]

ReturnType<T>

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

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

ThisParameterType

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 if —strictFunctionTypes is enabled. See #32964.

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

OmitThisParameter

Removes the ‘this’ parameter from a function type.

Note: This type only works correctly if —strictFunctionTypes is enabled. See #32964.

Example
  1. function toHex(this: Number) {
  2. return this.toString(16);
  3. }
  4. // The return type of `bind` is already using `OmitThisParameter`, this is just for demonstration.
  5. const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5);
  6. console.log(fiveToHex());

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.