Challenge - 5 Problems
Readonly Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code using Readonly?
Consider the following TypeScript code using the Readonly utility type. What will be logged to the console?
Typescript
interface User {
name: string;
age: number;
}
const user: Readonly<User> = { name: "Alice", age: 30 };
// user.age = 31; // Uncommenting this line causes an error
console.log(user.age);Attempts:
2 left
💡 Hint
Readonly makes all properties immutable, so you cannot change them after initialization.
✗ Incorrect
The Readonly utility type makes all properties of the object immutable. The code logs the original age value 30. Trying to assign a new value to user.age would cause a compilation error, but since that line is commented out, the code runs and logs 30.
🧠 Conceptual
intermediate1:30remaining
What does the Readonly utility type do in TypeScript?
Choose the best description of what the Readonly utility type does.
Attempts:
2 left
💡 Hint
Think about what 'readonly' means in everyday life.
✗ Incorrect
Readonly utility type marks all properties of a type as immutable, meaning you cannot change their values after initialization.
🔧 Debug
advanced2:00remaining
What error does this code produce?
Examine this TypeScript code snippet. What error will the compiler show?
Typescript
type Person = {
name: string;
age: number;
};
const person: Readonly<Person> = { name: "Bob", age: 25 };
person.age = 26;Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned new values.
✗ Incorrect
The Readonly utility type makes all properties read-only. Trying to assign a new value to person.age causes a TypeScript compile-time error indicating the property is read-only.
🚀 Application
advanced2:30remaining
How to make a nested object fully readonly?
Given this nested object type, which approach correctly makes all nested properties readonly in TypeScript?
Typescript
type Profile = {
user: {
name: string;
address: {
city: string;
zip: number;
};
};
};Attempts:
2 left
💡 Hint
Readonly only applies shallowly by default.
✗ Incorrect
The Readonly utility type only makes the first level properties readonly. To make nested properties readonly, you must explicitly mark each nested property as readonly or create a deep readonly type. Option A explicitly marks all nested properties readonly.
❓ Predict Output
expert2:30remaining
What is the output of this code using Readonly and mutation?
Analyze this TypeScript code. What will be the output when running it in JavaScript after compilation?
Typescript
type Data = {
value: number;
nested: {
count: number;
};
};
const data: Readonly<Data> = { value: 10, nested: { count: 5 } };
// data.value = 20; // Error: cannot assign to readonly
data.nested.count = 6;
console.log(data.nested.count);Attempts:
2 left
💡 Hint
Readonly is shallow; nested objects are not deeply frozen.
✗ Incorrect
Readonly only applies to the first level properties. The nested object itself is not readonly, so its properties can be changed. The code changes nested.count from 5 to 6 and logs 6.