0
0
Typescriptprogramming~20 mins

Readonly utility type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
Aundefined
B31
C30
DCompilation error
Attempts:
2 left
💡 Hint
Readonly makes all properties immutable, so you cannot change them after initialization.
🧠 Conceptual
intermediate
1:30remaining
What does the Readonly utility type do in TypeScript?
Choose the best description of what the Readonly utility type does.
AIt makes all properties of a type optional.
BIt converts all properties of a type to strings.
CIt removes all properties from a type.
DIt makes all properties of a type immutable (read-only).
Attempts:
2 left
💡 Hint
Think about what 'readonly' means in everyday life.
🔧 Debug
advanced
2: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;
ASyntaxError: Unexpected token '='.
BCannot assign to 'age' because it is a read-only property.
CNo error, code runs fine.
DReferenceError: person is not defined.
Attempts:
2 left
💡 Hint
Readonly properties cannot be assigned new values.
🚀 Application
advanced
2: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;
    };
  };
};
A
type ReadonlyProfile = {
  readonly user: {
    readonly name: string;
    readonly address: {
      readonly city: string;
      readonly zip: number;
    };
  };
};
Btype ReadonlyProfile = Readonly<Profile>;
Ctype ReadonlyProfile = Readonly<Profile> & Readonly<Profile['user']> & Readonly<Profile['user']['address']>;
Dtype ReadonlyProfile = Readonly<Profile> & { user: Readonly<Profile['user']> };
Attempts:
2 left
💡 Hint
Readonly only applies shallowly by default.
Predict Output
expert
2: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);
A6
B5
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Readonly is shallow; nested objects are not deeply frozen.