0
0
Typescriptprogramming~20 mins

Readonly properties in interfaces in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
ACompilation error due to assignment to readonly property
B{"id":1,"name":"Bob"}
CTypeError at runtime
D{"id":2,"name":"Bob"}
Attempts:
2 left
💡 Hint
Readonly properties prevent assignment in TypeScript but do not affect runtime JavaScript objects.
Predict Output
intermediate
2: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);
ARuntime error
B3
CCompilation error due to push on readonly array
D4
Attempts:
2 left
💡 Hint
Readonly applies to the reference, not the contents of the array.
🔧 Debug
advanced
2: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;
ACompilation error: Cannot assign to 'port' because it is a read-only property.
BRuntime error: Cannot assign to read-only property 'port'.
CNo error, port is updated to 3000.
DSyntax error: Missing semicolon.
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned after initialization in TypeScript.
📝 Syntax
advanced
2:00remaining
Which interface declaration correctly uses readonly properties?
Select the option that correctly declares an interface with readonly properties in TypeScript.
Ainterface Point { readonly x: number; readonly y: number; }
Binterface Point { x: readonly number; y: readonly number; }
Cinterface Point { readonly x: readonly number; y: readonly number; }
Dinterface Point { x: number readonly; y: number readonly; }
Attempts:
2 left
💡 Hint
The readonly keyword goes before the property name.
🚀 Application
expert
3: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);
ACompilation error due to assignment to nested readonly property
B{"email":"b@example.com","phone":"456"}
C{"email":"a@example.com","phone":"456"}
DRuntime error
Attempts:
2 left
💡 Hint
Readonly applies to each property individually, including nested ones.