Recall & Review
beginner
What is a read-only array in TypeScript?
A read-only array is an array that cannot be changed after creation. You cannot add, remove, or modify its elements.
Click to reveal answer
beginner
How do you declare a read-only array in TypeScript?
Use the
readonly keyword before the array type, like readonly number[] or use ReadonlyArray<number>.Click to reveal answer
beginner
Can you push or pop elements from a read-only array?
No, methods that change the array like
push or pop are not allowed on read-only arrays.Click to reveal answer
beginner
Why use read-only arrays?
They help prevent accidental changes to data, making your code safer and easier to understand.
Click to reveal answer
intermediate
How do you convert a normal array to a read-only array?
You can assign a normal array to a variable typed as a
readonly array, or use as const to make it read-only.Click to reveal answer
Which keyword is used to declare a read-only array in TypeScript?
✗ Incorrect
The
readonly keyword marks an array as read-only.What happens if you try to use
push on a read-only array?✗ Incorrect
Read-only arrays do not allow modification methods like
push, causing a compile-time error.Which of these types is equivalent to
readonly number[]?✗ Incorrect
ReadonlyArray<number> is the generic form of a read-only array.Can you reassign an element in a read-only array?
✗ Incorrect
Read-only arrays prevent changing elements after creation.
What does
as const do when used on an array?✗ Incorrect
as const makes the array and its elements read-only with literal types.Explain what a read-only array is and why it is useful in TypeScript.
Think about how you keep some lists fixed so no one changes them by mistake.
You got /4 concepts.
Describe how to declare and use a read-only array in TypeScript with an example.
Show how to write the code and what happens if you try to change it.
You got /4 concepts.