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 with strict mode?
Typescript
class Car { readonly brand: string; constructor(brand: string) { this.brand = brand; } changeBrand(newBrand: string) { this.brand = newBrand; } } const myCar = new Car('Toyota'); console.log(myCar.brand); myCar.changeBrand('Honda'); console.log(myCar.brand);
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned outside the constructor.
✗ Incorrect
In TypeScript, readonly properties can only be assigned during declaration or inside the constructor. Trying to assign them elsewhere causes a compile-time error.
🧠 Conceptual
intermediate2:00remaining
Readonly property behavior in subclasses
Consider this TypeScript code. What will be the output when running it?
Typescript
class Animal { readonly species: string = 'Unknown'; constructor(species?: string) { if (species) this.species = species; } } class Dog extends Animal { constructor() { super('Canine'); this.species = 'Dog'; } } const pet = new Dog(); console.log(pet.species);
Attempts:
2 left
💡 Hint
Readonly properties can only be assigned once, typically in the constructor of the declaring class.
✗ Incorrect
The readonly property species is assigned in the Animal constructor. Trying to assign it again in the Dog subclass constructor causes a compile-time error.
🔧 Debug
advanced2:00remaining
Why does this readonly property assignment fail?
This TypeScript code fails to compile. Identify the cause of the error.
Typescript
class Book { readonly title: string; constructor(title: string) { this.title = title; } rename(newTitle: string) { this.title = newTitle; } } const novel = new Book('1984'); novel.rename('Animal Farm');
Attempts:
2 left
💡 Hint
Readonly properties cannot be changed after initial assignment.
✗ Incorrect
The rename method tries to assign a new value to the readonly property title, which is not allowed and causes a compile-time error.
📝 Syntax
advanced2:00remaining
Identify the syntax error with readonly property
Which option contains a syntax error related to readonly class properties?
Attempts:
2 left
💡 Hint
Check where readonly properties are assigned.
✗ Incorrect
Option A tries to assign a new value to a readonly property inside a method, which is not allowed and causes a compile-time error.
🚀 Application
expert3:00remaining
Readonly properties with parameter properties
What will be the output of this TypeScript code?
Typescript
class Point { constructor(readonly x: number, readonly y: number) {} move(newX: number, newY: number) { this.x = newX; this.y = newY; } } const p = new Point(5, 10); console.log(`${p.x},${p.y}`); p.move(20, 30); console.log(`${p.x},${p.y}`);
Attempts:
2 left
💡 Hint
Readonly parameter properties cannot be reassigned after construction.
✗ Incorrect
The move method tries to assign new values to readonly properties x and y, which causes a compile-time error.