TypeScript Types
Readonly arrays — non-mutating array view
A readonly array type permits reading and non-mutating operations but omits mutating methods and disallows assignment through numeric indices. A mutable array may be passed to a readonly array parameter; the original owner can still change it. The modifier does not freeze the object or recursively protect elements. Example output: "1,2,3". The example was designed for strict checking with an ES2022 target; type annotations do not add runtime validation.
Formal Syntax & Structure
typescript syntaxtypescript
let values: readonly T[]; // or ReadonlyArray<T>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| T | type | Type of each readable array element. |
Working Example
Example Snippettypescript
const source = [1, 2];const view: readonly number[] = source;source.push(3);console.log(view.join(","));