Open In App

TypeScript - Recursive Types and Utility Types

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript adds strong typing to JavaScript. Recursive Types define types that refer to themselves, useful for trees or nested objects. Utility Types simplify type changes, like making properties optional or read-only. These tools help write clear and flexible code.

Recursive Types in TypeScript

A recursive type is a type that refers to itself in its definition. This allows us to model complex data structures such as trees, linked lists, and nested objects, where a type can be nested within itself.

  • Allow the definition of types that reference themselves.
  • Ideal for representing hierarchical or nested data.
  • Must use TypeScript’s type alias or interface to define recursive structures.

Syntax: Here's a simple example of a recursive type to represent a tree structure

type TreeNode = {
value: number;
children?: TreeNode[];
};
  • The TreeNode type has a value property and an optional children property.
  • The children property is an array of TreeNode, allowing for nested structures.

Using Recursive Types

JavaScript
const tree: TreeNode = {
    value: 1,
    children: [
        { value: 2 },
        {
            value: 3,
            children: [
                { value: 4 },
                { value: 5 }
            ]
        }
    ]
};

In this example, tree represents a hierarchical structure where nodes can have children, and those children can have their own children, and so on.

Benefits of Recursive Types

  • Model complex structures: Recursive types make it easy to represent hierarchical structures.
  • Type safety: TypeScript ensures that the recursive structure adheres to the correct types at every level of nesting.
  • Reusable types: Recursive types allow you to define structures that can be reused in multiple contexts.

To read more about it follow the article - Recursive Types & Interfaces

Utility Types in TypeScript

Utility types in TypeScript are built-in types that provide ready-made functionality for transforming or modifying other types. They help simplify common type manipulations, making TypeScript development more efficient.

Common Utility Types

1. Partial<T>

  • Makes all properties of a type optional.
  • Useful for creating objects where some properties may be missing.
type Partial<T> = {
[P in keyof T]?: T[P];
};

2. Required<T>

  • Makes all properties of a type required.
  • Helps enforce the presence of every property in an object.
type Required<T> = {
[P in keyof T]-?: T[P];
};

3. Readonly<T>

  • Makes all properties of a type read-only.
  • Prevents modification of object properties after initialization.
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};

4. Pick<T, K>

  • Selects a subset of properties from a type.
  • Useful when you need only a few properties from a complex type.
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};

5. Omit<T, K>

  • Removes specific properties from a type.
  • Can be used to exclude unwanted properties from a type.
type Omit<T, K extends keyof T> = {
[P in Exclude<keyof T, K>]: T[P];
};

6. Record<K, T>

  • Creates an object type with keys of type K and values of type T.
  • Useful for creating map-like structures.
type Record<K extends keyof any, T> = {
[P in K]: T;
};

7. Exclude<T, U>

  • Excludes from T all types that are assignable to U.
  • Helps filter out specific types from a union.
type Exclude<T, U> = T extends U ? never : T;

8. Extract<T, U>

  • Extracts types from T that are assignable to U.
  • Useful for narrowing types from a union.
type Extract<T, U> = T extends U ? T : never;

9. NonNullable<T>

  • Excludes null and undefined from a type.
  • Useful for ensuring that a value is neither null nor undefined.
type NonNullable<T> = T extends null | undefined ? never : T;

Benefits of Utility Types

  • Simplifies type transformations: Utility types provide built-in ways to change the structure of types (making properties optional, required, etc.).
  • Improves readability: They make your code cleaner and easier to understand, as the transformations are expressed with concise type keywords.
  • Increases productivity: You can avoid manually defining complex types when common transformations are required.

To read more about this follow the article - TypeScript Utility Types


Next Article

Similar Reads