TypeScript Object The ReadonlyArray Type
Last Updated :
28 Apr, 2025
In this article, we are going to learn about Object The ReadonlyArray Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the ReadonlyArray<T> type is used to create an immutable (read-only) array of elements of type T. It ensures that once an array is defined as ReadonlyArray, you cannot modify its contents or length. This is useful when you want to prevent accidental modifications to an array's values.
Syntax
const myArray: ReadonlyArray<T> = [...];
// or
const myArray: readonly T[] = [...];
Where-
- myArray is the variable name representing the read-only array.
- <T> is the type of element that the array can hold.
- ReadonlyArray is the utility itself, which ensures that the array should not modified after initialization.
- readonly is the utility itself instead of 'ReadonlyArray'. It is a shorthand.
- T[] is the type of data that will be in the array, It is a shorthand for '<T>.
Example 1: In this example, 'numbers' is declared as a ReadonlyArray<number>, so any attempts to modify the array (e.g., using push or assigning new values to elements) will result in TypeScript errors.
JavaScript
const numbers: ReadonlyArray<number> = [1, 2, 3, 4, 5];
console.log(numbers)
// You can assign readonly array by this also
//const str: readonly string[] = ['hello' , 'geekforgeeks']
//console.log(str)
// Attempting to modify the read-only
// array will result in a TypeScript error:
// numbers.push(6); // Error: Property 'push'
//does not exist on type 'readonly number[]'.
// numbers[0] = 0;
// Error: Index signature in type
//'readonly number[]' only permits reading.
Output:

Example 2: In TypeScript, the assignability between a regular array (T[]) and ReadonlyArray<T> is bidirectional. This means you can assign a regular array to a ReadonlyArray<T> and vice versa. In this example, regularArray is a regular array of numbers. readonlyArray is a ReadonlyArray<number> assigned the value of regularArray. You can assign a regular array to a ReadonlyArray. You can also assign a ReadonlyArray to a regular array, and modifying the regular array is allowed after the assignment.
JavaScript
const regularArray: number[] = [1, 2, 3];
// You can assign a regular
//array to a ReadonlyArray
const readonlyArray: ReadonlyArray<number> = regularArray;
// You can access elements from a ReadonlyArray
console.log(readonlyArray[0]); // Outputs: 1
const anotherReadonlyArray: ReadonlyArray<number> = [4, 5, 6];
// You can also assign a
// ReadonlyArray to a regular array
const regularArray2: number[] = [...anotherReadonlyArray];
// Attempting to modify a regular array after
// assignment from a ReadonlyArray is allowed
regularArray2.push(7);
console.log(regularArray2); // Outputs: [4, 5, 6, 7]
Output:
Similar Reads
TypeScript Object readonly Tuple Types In this article, we will learn about Object readonly Tuple Types in Typescript. In TypeScript, an object with read-only tuple types is a type that represents an object with properties where each property has a specific value and the order of properties is fixed, just like a tuple. However, unlike re
3 min read
TypeScript Object Type readonly Properties In TypeScript, the readonly modifier ensures that a property can be assigned a value only once during initialization and cannot be changed thereafter.This immutability helps prevent accidental modifications to object properties, enhancing code reliability.You can apply readonly to properties in clas
3 min read
TypeScript Object The Array Type In TypeScript, the Array Type is used to specify and enforce the type of elements that can be stored in an array, enhancing type safety during development. This adaptability ensures reusability across diverse data types, as exemplified by the Array type (e.g., number[] or string[]). Syntaxlet myArra
2 min read
TypeScript Object Tuple Types TypeScript provides various features to enhance type safety and developer productivity. One of these features includes Object Tuple Types. This feature ensures that an object follows a consistent shape in our code which results in a reduction in runtime errors and improves code quality. What are Obj
5 min read
TypeScript Readonly <Type> Utility Type In this article, we are going to learn about Readonly<Type> Utility Type in Typescript. Typescript is a popular programming language used for building scalable and robust applications. One of the features of Typescript is Readonly<Type> Utility Type which is used to create a new type whe
2 min read