Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. TypeScript Dictionary
  3. Readonly<T> / ReadonlyArray<T>

Readonly<T> / ReadonlyArray<T>

Since: TypeScript 2.0(2016)

Makes objects and arrays read-only (immutable). Readonly<T> marks all properties of an object as readonly, and ReadonlyArray<T> prevents mutating operations on arrays.

Syntax

type ReadonlyType = Readonly<OriginalType>;

// ReadonlyArray<T>: An array type that cannot be mutated.
let variableName: ReadonlyArray<Type> = [value, value];

// readonly Type[]: Shorthand equivalent to ReadonlyArray<T>.
let variableName: readonly Type[] = [value, value];

Syntax Overview

TypeDescription
Readonly<T>Returns a type with all properties of T marked as readonly. Reassignment to any property is disallowed.
ReadonlyArray<T>An immutable array type. Mutating methods such as push, pop, and splice are not available.
readonly Type[]Shorthand notation equivalent to ReadonlyArray<T>.
as constFixes a variable's type as a literal type and fully readonly. More restrictive than Readonly.

Sample Code

interface User {
	id: number;
	name: string;
	email: string;
}

type ReadonlyUser = Readonly<User>;
// Equivalent to: { readonly id: number; readonly name: string; readonly email: string; }

const user: ReadonlyUser = { id: 1, name: "Ryomen Sukuna", email: "ryomen_sukuna@wp-p.info" };
console.log(user.name); // Outputs "Ryomen Sukuna".
// user.name = "Gojo Satoru"; // Error: cannot assign to a readonly property.

// Using Readonly on a function parameter prevents the function from modifying the object.
function displayUser(user: Readonly<User>): void {
	console.log(`${user.id}: ${user.name}`);
	// user.name = "Changed"; // Error: cannot modify inside this function.
}
displayUser(user); // Outputs "1: Ryomen Sukuna".

// ReadonlyArray<T>: Prevents mutating operations on an array.
const scores: ReadonlyArray<number> = [85, 92, 78, 90];
console.log(scores[0]); // Outputs 85.
// scores.push(100); // Error: push does not exist on ReadonlyArray.
// scores[0] = 99; // Error: index assignment is also disallowed.

// readonly Type[]: Equivalent shorthand for ReadonlyArray.
const names: readonly string[] = ["Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi"];
console.log(names.length); // Outputs 3.

// Difference from as const: Readonly<T> is a shallow conversion.
interface Config {
	server: { host: string; port: number };
}
const config: Readonly<Config> = { server: { host: "localhost", port: 3000 } };
// config.server = { host: "other", port: 8080 }; // Error: server is readonly.
config.server.host = "other"; // Allowed — nested properties are not converted.

// as const performs a deep conversion.
const deepConfig = { server: { host: "localhost", port: 3000 } } as const;
// deepConfig.server.host = "other"; // Error: nested properties are also readonly.

console.log(deepConfig.server.port); // Outputs 3000.

Running the above produces the following output:

npx ts-node ts_readonly.ts
Ryomen Sukuna
1: Ryomen Sukuna
85
3
3000

Overview

Readonly<T> adds the readonly modifier to every property of an object type. It is implemented internally as the mapped type { readonly [K in keyof T]: T[K] }. This prevents reassignment to object properties at compile time, letting you express immutable data structures.

ReadonlyArray<T> (or readonly T[]) disallows mutating array operations — such as push, pop, splice, and sort — at the type level. Using it for configuration values or constant data passed to functions prevents those functions from accidentally modifying the array.

Note: Readonly<T> is a shallow conversion, so properties of nested objects are not made readonly. To make all nested levels readonly as well, use an as const assertion (see Literal Types).

If you find any errors or copyright issues, please .