0
0
Typescriptprogramming~5 mins

Read-only arrays in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aimmutable
Bconst
Cstatic
Dreadonly
What happens if you try to use push on a read-only array?
AIt throws a compile-time error
BIt adds the element successfully
CIt removes the last element
DIt converts the array to a normal array
Which of these types is equivalent to readonly number[]?
AArray&lt;number&gt;
BReadonlyArray&lt;number&gt;
Cnumber[]
Dconst number[]
Can you reassign an element in a read-only array?
AYes, anytime
BOnly if the array is declared with <code>const</code>
CNo, elements cannot be changed
DOnly if you use <code>as any</code>
What does as const do when used on an array?
AMakes the array read-only and its elements literal types
BMakes the array mutable
CDeletes the array
DConverts the array to a string
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.