TypeScript Object readonly Tuple Types
Last Updated :
28 Apr, 2025
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 regular tuples, you can also specify that the object is read-only, meaning that once the values are set, they cannot be modified.
Syntax
type ReadOnlyTupleObject = readonly [key1: Type1, key2: Type2, ...];
Parameters
- ReadOnlyTupleObject: The name of the type that represents the read-only tuple object.
- readonly: This keyword specifies that the object is read-only, meaning its properties cannot be modified once they are set.
- [key1: Type1, key2: Type2, ...]: Inside the square brackets, you define the properties of the object as a tuple. Each property is specified with a key (property name) and its associated type.
Example 1: In this example, We define a GFG type as a read-only tuple with two elements, both of type string. We create a variable, student, and assign them values that match the type GFG. When we attempt to modify student[0], TypeScript raises an error because the GFG type is read-only. student cannot be modified once its value is set.
JavaScript
type GFG = readonly [string, string];
const student: GFG = ['Akshit',
'Geeks Premier League 2023'];
console.log(student)
// Error: Cannot assign to '0'
// because it is a read-only property.
// student[0] = 'Aman';
Output:

If we uncomment student[0]='Aman' from the above example, we will get the following error. When we attempt to modify student[0], TypeScript raises an error because the GFG type is read-only. student cannot be modified once their values are set.

Example 2: In this example, we will see that array literals with const assertions will be inferred with readonly tuple types. We have defined two arrays, colors, and coordinates, and a function called displayColorAndCoordinates that accepts two parameters: color and coord. The types of these parameters are inferred based on the read-only tuple types, ensuring that you can only pass valid values from the respective arrays. When we call displayColorAndCoordinates(colors[0], coordinates[1]), we pass values from the read-only tuple types as arguments. TypeScript ensures type safety and only valid values from the arrays can be passed to the function. When we try to modify these inferred read-only tuple types (e.g., colors[0] = 'yellow' or coordinates.push(40)), TypeScript raises errors, indicating that these properties are read-only and cannot be modified.
JavaScript
// Using array literals with const assertion
// to infer read-only tuple types
const colors = ['red', 'green', 'blue'] as const;
const coordinates = [10, 20, 30] as const;
// Function that accepts the inferred
// read-only tuple types
function displayColorAndCoordinates(
color: typeof colors[number],
coord: typeof coordinates[number]
) {
console.log(`Color: ${color}`);
console.log(`Coordinates: ${coord}`);
}
// Call the function with values
// from the read-only tuple types
displayColorAndCoordinates(colors[0], coordinates[1]);
// Try to modify the inferred read-only tuple types
// Error: Cannot assign to '0' because
// it is a read-only property.
// colors[0] = 'yellow';
// Error: Property 'push' does not
// exist on type 'readonly [10, 20, 30]'.
// coordinates.push(40);
Output:

Reference:
Similar Reads
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 Object The ReadonlyArray Type 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 ty
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 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
TypeScript Object Type Index Signatures In this article, we are going to learn about Object Type Index Signatures in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, index signatures allow you to define object types with dynamic keys, where the keys can be of a spe
2 min read
Explain the Tuple Types in TypeScript TypeScript is an open-source object-oriented programming language developed and maintained by Microsoft Corporation. It is a strongly typed language and was first introduced in 2012. TypeScript is a strict superset of JavaScript, which means that anything implemented in JavaScript can be implemented
3 min read
How to Create Deep Readonly Type in Typescript? In TypeScript, the readonly access modifier is a powerful tool that ensures immutability by marking properties of a class as immutable. Once a property is markedreadonly, it cannot be reassigned. This is highly useful for maintaining consistent and safe data structures, especially in scenarios such
3 min read
TypeScript Object Type Property Modifiers TypeScript Type Property Modifiers are used to specify each property in an object such as: the type, whether the property is optional, and whether the property can be written to. TypeScript Object Type Property Modifiers:readonly Properties: An object type with readonly properties specifies that the
3 min read
TypeScript Mapped Types Mapped types in TypeScript allow you to create new types by transforming the properties of existing types.They enable modifications like making properties optional, read-only, or altering their types.Mapped types help reduce code duplication and enhance type safety by automating type transformations
3 min read
Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim
3 min read