0
0
Typescriptprogramming~10 mins

Read-only arrays in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Read-only arrays
Declare array with readonly type
Try to read elements
Allowed
Try to modify elements
Compile error: modification not allowed
Use array safely without changes
End
You create an array that cannot be changed. Reading is allowed, but any change causes an error.
Execution Sample
Typescript
const nums: readonly number[] = [1, 2, 3];
console.log(nums[0]);
// nums[1] = 5; // Error: cannot assign to readonly array
const newNums = nums.map(x => x * 2);
console.log(newNums);
This code shows a readonly array, reading elements, and creating a new array by mapping.
Execution Table
StepActionCode LineResultNotes
1Declare readonly array numsconst nums: readonly number[] = [1, 2, 3];nums = [1, 2, 3]Array created, cannot be changed
2Read first elementconsole.log(nums[0]);Output: 1Reading allowed
3Attempt to modify element// nums[1] = 5;Compile errorModification not allowed on readonly array
4Create new array by mapconst newNums = nums.map(x => x * 2);newNums = [2, 4, 6]map returns new array, original unchanged
5Print newNumsconsole.log(newNums);Output: [2, 4, 6]Shows doubled values
💡 Execution stops because modification line is commented out due to compile error
Variable Tracker
VariableStartAfter Step 1After Step 4Final
numsundefined[1, 2, 3][1, 2, 3][1, 2, 3]
newNumsundefinedundefined[2, 4, 6][2, 4, 6]
Key Moments - 3 Insights
Why can't we assign a new value to nums[1]?
Because nums is declared as a readonly array, any attempt to change its elements causes a compile error, as shown in step 3 of the execution table.
Can we read elements from a readonly array?
Yes, reading elements is allowed and safe, as shown in step 2 where nums[0] is read and printed.
How can we create a changed version of a readonly array?
By using methods like map that return a new array without modifying the original, as shown in step 4 where newNums is created.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of nums after step 4?
A[2, 4, 6]
Bundefined
C[1, 2, 3]
DCompile error
💡 Hint
Check the variable_tracker row for nums after step 4
At which step does the code try to modify the readonly array?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for modification attempts
If we remove the comment from the modification line, what happens?
AThe array nums changes to [1, 5, 3]
BA compile error occurs
CThe program prints 5
DThe map function fails
💡 Hint
Refer to the 'Result' column in step 3 of the execution_table
Concept Snapshot
Readonly arrays in TypeScript:
- Declared with 'readonly' keyword
- Elements can be read but not changed
- Any modification causes compile error
- Methods like map return new arrays
- Useful for safe, immutable data
Full Transcript
This visual trace shows how readonly arrays work in TypeScript. First, we declare an array with the readonly keyword, which means we can read elements but cannot change them. Reading nums[0] prints 1 successfully. Trying to assign a new value to nums[1] causes a compile error, so that line is commented out. We then use the map method to create a new array with doubled values, which is allowed because it does not change the original array. The new array is printed as [2, 4, 6]. The variable tracker confirms nums stays unchanged throughout. Key moments clarify why modification is disallowed and how to work with readonly arrays safely. The quiz tests understanding of these steps.