Bird
Raised Fist0
Typescriptprogramming~20 mins

Constructor parameter types in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does specifying parameter types in a TypeScript class constructor do?
easy
A. It changes the class name based on parameters.
B. It runs the constructor faster at runtime.
C. It automatically creates getter and setter methods.
D. It tells what kind of values the constructor expects when creating an object.

Solution

  1. Step 1: Understand constructor parameter types

    Constructor parameter types define what type of values the constructor expects to receive when creating an instance of the class.
  2. Step 2: Identify the purpose of parameter types

    They help TypeScript check for errors before running the code by ensuring the right types are passed.
  3. Final Answer:

    It tells what kind of values the constructor expects when creating an object. -> Option D
  4. Quick Check:

    Constructor parameter types = Define expected input types [OK]
Hint: Constructor types define expected inputs clearly [OK]
Common Mistakes:
  • Thinking parameter types speed up runtime
  • Confusing parameter types with method creation
  • Believing parameter types rename classes
2. Which of the following is the correct way to declare a constructor with typed parameters in TypeScript?
easy
A. constructor(name: string, age: number) {}
B. constructor(name string, age number) {}
C. constructor(name, age) : string, number {}
D. constructor(name => string, age => number) {}

Solution

  1. Step 1: Review TypeScript constructor syntax

    In TypeScript, parameter types are declared after the parameter name with a colon, like name: string.
  2. Step 2: Check each option

    constructor(name: string, age: number) {} uses correct syntax with colons and types. Options A, C, and D use invalid syntax.
  3. Final Answer:

    constructor(name: string, age: number) {} -> Option A
  4. Quick Check:

    Parameter types use colon syntax [OK]
Hint: Use colon after parameter name for types [OK]
Common Mistakes:
  • Omitting colons between name and type
  • Using commas or arrows instead of colons
  • Placing types after parentheses
3. What will be the output of this TypeScript code?
class Person {
  constructor(public name: string, private age: number) {}
  getAge() { return this.age; }
}
const p = new Person('Alice', 30);
console.log(p.name);
console.log(p.getAge());
medium
A. Error: age is private
B. Alice 30
C. Alice undefined
D. undefined 30

Solution

  1. Step 1: Understand public and private in constructor

    The public name creates a public property accessible outside. The private age creates a private property accessible only inside the class.
  2. Step 2: Check console.log outputs

    p.name prints 'Alice' because it's public. p.getAge() returns 30 because the method accesses private age internally.
  3. Final Answer:

    Alice 30 -> Option B
  4. Quick Check:

    Public accessible, private via method [OK]
Hint: Public props accessible directly, private via methods [OK]
Common Mistakes:
  • Expecting private properties accessible outside
  • Confusing undefined with private access
  • Thinking private causes runtime error
4. Identify the error in this TypeScript class constructor:
class Car {
  constructor(make: string, year: number) {
    this.make = make;
    this.year = year;
  }
}
medium
A. Constructor must have return type declared
B. Cannot assign parameters to 'this' inside constructor
C. Class properties 'make' and 'year' are missing
D. Missing type for parameter 'year' in constructor

Solution

  1. Step 1: Check class properties and constructor assignments

  2. Step 2: Check class properties

    The class does not declare properties 'make' and 'year' before assigning them in the constructor, which causes an error: Property 'make' does not exist on type 'Car'.
  3. Final Answer:

    Class properties 'make' and 'year' are missing -> Option C
  4. Quick Check:

    Declare class properties before use [OK]
Hint: Declare properties before assigning in constructor [OK]
Common Mistakes:
  • Ignoring missing property declarations
  • Thinking constructor parameters auto-create properties
  • Expecting return type in constructor
5. You want to create a class Book that takes a title (string), pages (number), and an optional author (string). Which constructor declaration correctly types these parameters and allows creating a Book by omitting the author argument (leaving author as undefined)?
hard
A. constructor(title: string, pages: number, author?: string) {}
B. constructor(title: string, pages: number, author: string = '') {}
C. constructor(title: string, pages: number, author: string) {}
D. constructor(title: string, pages: number, author?: string = '') {}

Solution

  1. Step 1: Understand optional parameters in TypeScript

    Optional parameters are marked with a question mark ? after the parameter name, allowing them to be omitted when calling the constructor, leaving the parameter undefined.
  2. Step 2: Evaluate each option

    constructor(title: string, pages: number, author?: string = '') {}: author?: string = '' -- omitting sets to '', not undefined. constructor(title: string, pages: number, author: string = '') {}: author: string = '' -- omitting sets to ''. constructor(title: string, pages: number, author: string) {} requires author. constructor(title: string, pages: number, author?: string) {}: author?: string -- omitting leaves undefined.
  3. Final Answer:

    constructor(title: string, pages: number, author?: string) {} -> Option A
  4. Quick Check:

    Optional params use ? after name [OK]
Hint: Use ? after parameter name for optional [OK]
Common Mistakes:
  • Using default value without ? for optional
  • Forgetting to mark optional parameters
  • Mixing optional and default syntax incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard