0
0
Typescriptprogramming~20 mins

Why typed classes matter in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Typed Classes Mastery
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 typed class example?

Consider this TypeScript class with typed properties and a method. What will be printed when the code runs?

Typescript
class Person {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  greet(): string {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  }
}

const p = new Person("Alice", 30);
console.log(p.greet());
A"Hello, my name is Alice and I am 30 years old."
BSyntaxError: Unexpected token
CTypeError: Cannot read property 'name' of undefined
D"Hello, my name is undefined and I am undefined years old."
Attempts:
2 left
💡 Hint

Look at how the constructor assigns values to the typed properties.

🧠 Conceptual
intermediate
1:30remaining
Why does TypeScript require types in classes?

Which of the following best explains why typed classes are important in TypeScript?

AThey make the code run faster at runtime by optimizing memory.
BThey automatically generate user interfaces based on class definitions.
CThey allow classes to be used only in browsers that support types.
DThey help catch errors early by ensuring properties and methods have expected types.
Attempts:
2 left
💡 Hint

Think about what types help with before running the code.

🔧 Debug
advanced
2:00remaining
What error does this typed class code produce?

Examine this TypeScript class code. What error will occur when compiling?

Typescript
class Car {
  model: string;
  year: number;

  constructor(model: string) {
    this.model = model;
  }

  getAge(currentYear: number): number {
    return currentYear - this.year;
  }
}
AProperty 'year' has no initializer and is not definitely assigned in the constructor.
BTypeError: Cannot read property 'year' of undefined
CSyntaxError: Unexpected token in constructor
DNo error, code compiles successfully
Attempts:
2 left
💡 Hint

Check if all typed properties are assigned values in the constructor.

📝 Syntax
advanced
1:30remaining
Which option correctly defines a typed class with a readonly property?

Choose the correct TypeScript class definition where id is a readonly number property set in the constructor.

Aclass User { id: readonly number; constructor(id: number) { this.id = id; } }
Bclass User { readonly id: number; constructor(id: number) { this.id = id; } }
Cclass User { readonly id; constructor(id: number) { this.id = id; } }
Dclass User { id: number readonly; constructor(id: number) { this.id = id; } }
Attempts:
2 left
💡 Hint

Remember the correct order of modifiers and types in TypeScript.

🚀 Application
expert
2:30remaining
How many properties does this typed class instance have?

Given this TypeScript class and instance, how many own properties does the instance book have?

Typescript
class Book {
  title: string;
  author: string;
  static category: string = "Literature";

  constructor(title: string, author: string) {
    this.title = title;
    this.author = author;
  }
}

const book = new Book("1984", "George Orwell");
const count = Object.keys(book).length;
console.log(count);
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Static properties are not part of instance own properties.