📖 Scenario: You are creating a simple program to manage a user's profile information. You want to make sure that once the profile data is set, it cannot be changed accidentally.
🎯 Goal: Learn how to use the Readonly utility type in TypeScript to make an object immutable.
📋 What You'll Learn
Create an object with user profile data
Create a readonly version of the user profile object
Try to update the readonly object (should cause error)
Print the readonly user profile object
💡 Why This Matters
🌍 Real World
Readonly types help prevent accidental changes to important data like user profiles, settings, or configuration objects in real applications.
💼 Career
Understanding Readonly and other utility types is important for writing safe and maintainable TypeScript code in professional software development.
Progress0 / 4 steps
1
Create a user profile object
Create a variable called userProfile with type { name: string; age: number; } and assign it the values { name: 'Alice', age: 30 }.
Create a variable called readonlyUserProfile with type Readonly<typeof userProfile> and assign it the value of userProfile.
Typescript
Hint
Use const readonlyUserProfile: Readonly<typeof userProfile> = userProfile;
3
Try to update the readonly user profile
Add a line that tries to change readonlyUserProfile.age to 31. This should cause a TypeScript error because the object is readonly.
Typescript
Hint
Write readonlyUserProfile.age = 31; to see the error.
4
Print the readonly user profile
Write console.log(readonlyUserProfile) to display the readonly user profile object.
Typescript
Hint
Use console.log(readonlyUserProfile); to print the object.
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?