Read-Only Array and Tuple Types in TypeScript

Danielle Dias
Level Up Coding
Published in
3 min readApr 10, 2023

--

Photo by Eugene Zhyvchik on Unsplash

Among TypeScript’s many features are read-only array and tuple types, which provide a way to enforce immutability on your collections, ensuring that they cannot be modified once created.

Read-Only Array Types

What is a Read-Only Array Type?

A read-only array type is a version of the standard array type that prevents modification of its elements. This means that once you have created a read-only array, you cannot add, remove, or change its elements.

In TypeScript, you can create a read-only array type using the ReadonlyArray<T> utility type or the readonly modifier with a standard array type.

Using the ReadonlyArray<T> utility type:

const numbers: ReadonlyArray<number> = [1, 2, 3, 4, 5];

Using the readonly modifier with a standard array type:

const numbers: readonly number[] = [1, 2, 3, 4, 5];

In both cases, you end up with a read-only array of numbers.

Benefits of Using Read-Only Array Types

There are several benefits to using read-only array types in TypeScript:

  1. Immutability: By enforcing immutability on your arrays, you can prevent unintended modifications, making your code more predictable and easier to reason about.
  2. Encapsulation: Read-only array types help you create a clear boundary between components or functions that are responsible for modifying the array and those that should only read from it.
  3. Enhanced type safety: Read-only arrays provide better type safety by preventing accidental mutations.

Limitations of Read-Only Array Types

  1. Compatibility: Since read-only arrays are a TypeScript-specific feature, they may not be well-supported by some JavaScript libraries or utilities.

Read-Only Tuple Types

What is a Read-Only Tuple Type?

A read-only tuple type is a version of the standard tuple type that prevents modification of its elements. Like read-only arrays, read-only tuples ensure that their elements cannot be modified once created.

In TypeScript, you can create a read-only tuple type using the readonly modifier with a standard tuple type.

const point: readonly [number, number] = [1, 2];

In this example, you have a read-only tuple representing a point with two number elements.

Benefits of Using Read-Only Tuple Types

There are several benefits to using read-only tuple types in TypeScript:

  1. Immutability: Similar to read-only arrays, read-only tuples enforce immutability, making your code more predictable and easier to reason about.
  2. Enhanced type safety: Read-only tuples provide better type safety by preventing accidental mutations.

Advanced Usage

Mapped Types with Read-Only Arrays and Tuples

Mapped types in TypeScript are a powerful feature that allows you to create new types by transforming the properties of an existing type. You can use mapped types to create read-only versions of custom arrays or tuple types.

For example, consider the following custom tuple type Person:

type Person = [string, number, string];

You can create a read-only version of this tuple type using a mapped type:

type ReadonlyPerson = { readonly [K in keyof Person]: Person[K] };
const person: ReadonlyPerson = ['Alice', 30, 'Developer'];

Conditional Types with Read-Only Arrays and Tuples

Conditional types in TypeScript allow you to create new types based on the conditions applied to existing types. You can use conditional types to create read-only versions of custom arrays or tuple types that depend on other types. For example:

type DeepReadonlyArray<T> = T extends Array<infer U>
? ReadonlyArray<DeepReadonlyArray<U>>
: T;

This type alias creates a deeply read-only array type, which ensures that all nested arrays are also read-only. You can use this type alias to create deeply read-only arrays and tuples:

const matrix: DeepReadonlyArray<[number, number][]> = [
[1, 2],
[3, 4],
[5, 6],
];

// All nested arrays are now read-only.

Conclusion

In this article, we explored the read-only array and tuple types in TypeScript, discussing their purpose, usage, benefits, and limitations. Hope this article is helpful to you.

Thanks for reading. If you enjoyed this article, consider supporting me by becoming a Medium member.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--