What does the Readonly utility type do in TypeScript?
It makes all properties of an object type read-only, so they cannot be changed after initialization.
Click to reveal answer
beginner
How do you apply the Readonly utility type to an interface Person?
You write Readonly<Person> to create a new type where all properties of Person are read-only.
Click to reveal answer
beginner
Can you modify properties of an object typed with Readonly after it is created?
No, properties are read-only and TypeScript will give an error if you try to assign new values.
Click to reveal answer
intermediate
Example: What is the error in this code?<br><pre>type User = { name: string };
const user: Readonly<User> = { name: 'Alice' };
user.name = 'Bob';</pre>
Error: Cannot assign to 'name' because it is a read-only property.
Click to reveal answer
beginner
Why use Readonly utility type in your code?
To prevent accidental changes to objects, making code safer and easier to understand by enforcing immutability.
Click to reveal answer
What does the Readonly utility type do?
AConverts properties to optional
BMakes all properties of an object read-only
CAllows properties to be changed anytime
DRemoves all properties from an object
✗ Incorrect
Readonly makes all properties read-only, preventing changes.
How do you declare a read-only version of a type Car?
AReadonly<Car>
BMutable<Car>
CPartial<Car>
DRequired<Car>
✗ Incorrect
Readonly creates a type with all properties read-only.
What happens if you try to assign a new value to a read-only property?
AThe value changes successfully
BThe property becomes optional
CThe property is deleted
DTypeScript shows an error
✗ Incorrect
TypeScript prevents assignment to read-only properties with an error.
Can Readonly be used on nested objects automatically?
AIt removes nested objects
BYes, it applies deeply by default
CNo, it only applies to the first level properties
DIt converts nested objects to arrays
✗ Incorrect
Readonly only makes the top-level properties read-only, nested objects remain mutable unless handled separately.
Explain what the Readonly utility type does and why it is useful.
Think about how it stops you from changing object properties after creation.
You got /4 concepts.
Describe how you would use Readonly with a TypeScript interface and what happens if you try to modify a property.
Consider the syntax and the error you get when changing a read-only property.
You got /4 concepts.
Practice
(1/5)
1. What does the Readonly utility type do in TypeScript?
easy
A. Makes all properties optional
B. Allows properties to be changed only once
C. Removes all properties from an object type
D. Makes all properties of an object type unchangeable
Solution
Step 1: Understand the purpose of Readonly
The Readonly utility type is designed to make all properties of an object type immutable, meaning they cannot be changed after initialization.
Step 2: Compare with other options
Options B, C, and D describe behaviors that are not related to Readonly. Only Makes all properties of an object type unchangeable correctly describes making properties unchangeable.
Final Answer:
Makes all properties of an object type unchangeable -> Option D
Quick Check:
Readonly = makes properties unchangeable [OK]
Hint: Readonly means no changes allowed to object properties [OK]
Common Mistakes:
Thinking Readonly allows one-time changes
Confusing Readonly with Partial or Required types
Believing Readonly removes properties
2. Which of the following is the correct syntax to create a readonly type from an interface User?
easy
A. type ReadonlyUser = Readonly<User>();
B. type ReadonlyUser = Readonly<User>;
C. type ReadonlyUser = readonly User;
D. type ReadonlyUser = Readonly<User>[];
Solution
Step 1: Recall the syntax of Readonly utility
The correct syntax to create a readonly version of a type or interface is Readonly<TypeName> without parentheses or array brackets.
Step 2: Analyze each option
type ReadonlyUser = Readonly<User>; uses the correct generic syntax. type ReadonlyUser = Readonly<User>(); incorrectly uses parentheses. type ReadonlyUser = readonly User; uses lowercase 'readonly' which is a keyword for arrays or tuples, not for types. type ReadonlyUser = Readonly<User>[]; creates an array of readonly users, which is different.
Final Answer:
type ReadonlyUser = Readonly<User>; -> Option B
Quick Check:
Readonly syntax = Readonly<Type> [OK]
Hint: Use Readonly<Type> without parentheses or brackets [OK]
Common Mistakes:
Adding parentheses after Readonly
Using lowercase 'readonly' instead of utility type
Confusing array of readonly with readonly type
3. What will be the output of the following TypeScript code?