Challenge - 5 Problems
Readonly Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of readonly property assignment
What will be the output of this TypeScript code when compiled and run in JavaScript?
Typescript
interface User {
readonly id: number;
name: string;
}
const user: User = { id: 1, name: "Alice" };
user.name = "Bob";
// user.id = 2; // Uncommenting this line causes error
console.log(user);Attempts:
2 left
💡 Hint
Readonly properties prevent assignment in TypeScript but do not affect runtime JavaScript objects.
✗ Incorrect
The readonly modifier in TypeScript prevents assignment to the property during compilation. However, at runtime, JavaScript does not enforce readonly, so changing 'name' works fine. The commented line would cause a compile-time error if uncommented. The output shows the updated name but the original id.
❓ Predict Output
intermediate2:00remaining
Readonly array property behavior
Given this interface and code, what is the output?
Typescript
interface Data {
readonly values: number[];
}
const d: Data = { values: [1, 2, 3] };
d.values.push(4);
console.log(d.values.length);Attempts:
2 left
💡 Hint
Readonly applies to the reference, not the contents of the array.
✗ Incorrect
The 'readonly' keyword on the array property means the reference to the array cannot be changed, but the array's contents can still be modified. So push adds an element, increasing length to 4.
🔧 Debug
advanced2:00remaining
Why does this readonly property assignment cause an error?
Identify the error in this code and explain why it happens.
Typescript
interface Config {
readonly port: number;
}
const config: Config = { port: 8080 };
config.port = 3000;Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned after initialization in TypeScript.
✗ Incorrect
The assignment to 'port' after initialization violates the readonly constraint, causing a compile-time error.
📝 Syntax
advanced2:00remaining
Which interface declaration correctly uses readonly properties?
Select the option that correctly declares an interface with readonly properties in TypeScript.
Attempts:
2 left
💡 Hint
The readonly keyword goes before the property name.
✗ Incorrect
In TypeScript, readonly is placed before the property name. Options B, C, and D use invalid syntax.
🚀 Application
expert3:00remaining
Effect of readonly on nested objects in interfaces
Consider this interface and code. What will be the output?
Typescript
interface Profile {
readonly id: number;
details: {
readonly email: string;
phone: string;
};
}
const p: Profile = { id: 1, details: { email: "a@example.com", phone: "123" } };
p.details.phone = "456";
// p.details.email = "b@example.com"; // Uncommenting causes error
console.log(p.details);Attempts:
2 left
💡 Hint
Readonly applies to each property individually, including nested ones.
✗ Incorrect
The 'phone' property is not readonly, so it can be changed. The 'email' property is readonly, so assigning to it causes a compile-time error if uncommented. The output shows the updated phone and original email.