TypeScriptDev & Eng

Content type: Hands-on

2 min read

TypeScript: ReadonlyArray

Article · Dev & Eng
Reading
2 min
Code
4
In short

Continuing my series of articles about TypeScript, today I'll write about the access modifier added in version 3.4 for arrays and tuples, readonly.

Continuing my series of articles about TypeScript, today I'll write about an access modifier added in version 3.4 for arrays and tuples, readonly.

But before talking about it, let's take a look at the type ReadonlyArray<T>.

For those who don't know it, readonlyArray<T> removes all mutation methods from an array, such as: push, pop… etc.

Below you have an example of how to use it:

Expert views

What people are saying about it.

JetBrains has confirmed that CVE-2026-114938, nicknamed Java4Shell, affects the JBR embedded in all its IDEs as well as the JDK running in production. There's a patch and a workaround, but they are separate fixes.

Bisneto Bragain an iMasters article · Read the article: Java4Shell vulnerability exposes JVM in production: here's how to fix it now

Oracle's November 2026 CSPU brings ten fixes for MySQL, three of them exploitable over the network without login. Percona has already merged the fixes into version 8.4 and details what can be mitigated now.

Roberto Dinizin an iMasters article · Read the article: Oracle fixes three critical MySQL flaws exploitable without authentication

Kubernetes 1.38, expected on December 16, 2026, drops environment variables used in scripts, retires a legacy cgroup mechanism, and changes swap behavior. Here's what to review before upgrading your cluster.

Rafael Oliveirain an iMasters article · Read the article: Kubernetes 1.38 comes with changes that can break CI/CD and legacy nodes
javascript
let arrNumber: number[] = [1, 2, 3, 4];
let readOnlyArr: ReadonlyArray<number> = arrNumber;
readOnlyArr[0] = 12; // error!
readOnlyArr.push(5); // error!
readOnlyArr.length = 100; // error!
a = readOnlyArr; // error!

Note that on lines 2, 3, 4, 5 and 6 we can't change its value.

Now that we're aligned on ReadonlyArray<T>, let's move to the readonly. modifier.

It works the same way as we saw in the previous example, the only difference is its syntax, which in this new version is simpler, and we can also use it with tuples.

In case you haven't seen anything about tuples in TypeScript, I recommend reading the following article: TypeScript Tuplas.

Now let's change the ReadonlyArray<T> from the previous example to readonly and iterate through it using for.

Below you have a code snippet demonstrating this change:

javascript
let arrNumber: number[] = [1, 2, 3, 4];
let readOnlyArr: Readonly<number[]> = arrNumber;


for (let index = 0; index < readOnlyArr.length; index++) {
    const element = readOnlyArr[index];
    console.log(element);
}

/*
Resultado:
1
2
3
4
*/

As you can see in the example above, we changed from ReadOnlyArray to readonly and it worked the same way.

And as I mentioned above, in this version we can also use the readonly modifier with tuples:

javascript

class Calc {
    somaComTuplas(numbs: readonly[number, number]) {
    const x = numbs[0] + numbs[1];
    return x;
 }
}


const readonlyTuple: readonly[number, number] =[10, 5];
let c = new Calc();
console.log(c.somaComTuplas(readonlyTuple));

Result: 15

Then comes that question, where could I use it, or where could I find a practical example using it?

Thinking about that, I did some research on a few libraries on the NPM portal and found a library that exports json data to a .csv file called json2csv.

Looking at its calls I found the method below:

javascript
export function parseAsync<T>(
    data: Readonly<T> | ReadonlyArray<T> | Readable,
    opts?: json2csv.Options<T>,
    transformOpts?: TransformOptions
): Promise<string>;

Note that it takes readonlyArray<T>. as a parameter.

Well, the idea of this article was to demonstrate one more of the new features in TypeScript 3.4 through a few practical examples 😉

I hope you enjoyed it, and see you in a next article, folks 😉

Translated from the Brazilian Portuguese original · Read the original

Read also