Readonly Utility Type

What

The Readonly<T> utility type creates a new version of a type where all top-level properties are marked as readonly.

How

You wrap the target type in Readonly:

interface Config {
  apiUrl: string;
  timeout: number;
}

const appConfig: Readonly<Config> = {
  apiUrl: "https://api.com",
  timeout: 5000
};

// Error: Cannot assign to 'timeout' because it is a read-only property
appConfig.timeout = 1000;

Why

Readonly is a safeguard against accidental mutation. It is particularly useful for configuration objects, state snapshots, or constants that are defined once and should never be changed throughout the lifecycle of the application.

Summary

Use Readonly<T> to protect your data structures from unintended side effects.