Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What class does ReadonlyArray inherit?

Tags:

typescript

I have this code snippet in TypeScript:

const arr: ReadonlyArray<string> = [
    "123",
    "234"
];

arr.push("345");

TS compiler shows an error:

Property 'push' does not exist on type 'ReadonlyArray<string>'.

Of course, it's clear that I cannot change/alter this array. The question is: what is the base class for ReadonlyArray? Does it inherit from standard Array class?

like image 494
shohrukh Avatar asked Nov 20 '25 05:11

shohrukh


1 Answers

https://www.typescriptlang.org/docs/handbook/interfaces.html states that:

TypeScript comes with a ReadonlyArray<T> type that is the same as Array<T> with all mutating methods removed, so you can make sure you don’t change your arrays after creation

If you want the source, head over to:

https://github.com/Microsoft/TypeScript/blob/master/src/lib/es5.d.ts#L1121 (01.06.2022) - Search for "ReadonlyArray" if source file evolved.

(its definition is on the line 1121 in the current version).

like image 81
Patrick Artner Avatar answered Nov 24 '25 22:11

Patrick Artner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!