0
0
Typescriptprogramming~20 mins

Constructor parameter types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor 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 TypeScript class constructor?

Consider this TypeScript class with typed constructor parameters. What will be the output when creating an instance and logging its properties?

Typescript
class Person {
  name: string;
  age: number;
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}
const p = new Person('Alice', 30);
console.log(`${p.name} is ${p.age} years old.`);
AAlice is 30 years old.
Bundefined is undefined years old.
CError: Property 'name' does not exist on type 'Person'.
DAlice is undefined years old.
Attempts:
2 left
💡 Hint

Check how the constructor assigns the parameters to the class properties.

Predict Output
intermediate
2:00remaining
What error occurs with this constructor parameter type mismatch?

What error will TypeScript show for this class when trying to create an instance?

Typescript
class Car {
  model: string;
  year: number;
  constructor(model: string, year: number) {
    this.model = model;
    this.year = year;
  }
}
const c = new Car('Tesla', '2020');
AProperty 'year' is missing in type 'string'.
BNo error, instance created successfully.
CType 'string' is not assignable to type 'number'.
DSyntaxError: Unexpected string.
Attempts:
2 left
💡 Hint

Look at the type of the second argument passed to the constructor.

🔧 Debug
advanced
2:00remaining
Why does this constructor cause a runtime error?

Examine the class and its constructor. Why does this code cause a runtime error?

Typescript
class Rectangle {
  width: number;
  height: number;
  constructor(width: number, height: number) {
    this.width = width;
    // Forgot to assign height
  }
  area() {
    return this.width * this.height;
  }
}
const r = new Rectangle(5, 10);
console.log(r.area());
AOutput: NaN
BOutput: 50
CRuntime error: Cannot read property 'height' of undefined.
DSyntaxError: Missing assignment in constructor.
Attempts:
2 left
💡 Hint

Check if all properties are initialized before use.

📝 Syntax
advanced
2:00remaining
Which constructor syntax is correct for parameter properties?

Which option correctly uses TypeScript's parameter properties to declare and initialize class members in the constructor?

A
class Point {
  constructor(x: number, y: number) {
    this.x: number = x;
    this.y: number = y;
  }
}
B
class Point {
  constructor(x: number, y: number) {
    public this.x = x;
    private this.y = y;
  }
}
C
class Point {
  constructor(public x: number, y: number) {
    this.y = y;
  }
}
D
class Point {
  constructor(public x: number, private y: number) {}
}
Attempts:
2 left
💡 Hint

Parameter properties combine declaration and assignment in the constructor parameters.

🚀 Application
expert
2:00remaining
How many properties does this class instance have after construction?

Given this TypeScript class using parameter properties and explicit members, how many own properties will an instance have?

Typescript
class User {
  id: number;
  constructor(public name: string, id: number) {
    this.id = id;
  }
}
const u = new User('Bob', 42);
console.log(Object.keys(u).length);
A1
B2
C0
D3
Attempts:
2 left
💡 Hint

Parameter properties create class members that are own properties. Explicit assignments also create own properties.