Bird
Raised Fist0
Typescriptprogramming~20 mins

ConstructorParameters type in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ConstructorParameters 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 code using ConstructorParameters?

Consider this TypeScript code using ConstructorParameters. What will be logged?

Typescript
class Person {
  constructor(public name: string, public age: number) {}
}

type Params = ConstructorParameters<typeof Person>;

const args: Params = ['Alice', 30];
const p = new Person(...args);
console.log(p.name, p.age);
A"Person { name: 'Alice', age: 30 }"
B"Alice 30"
C"['Alice', 30]"
D"undefined undefined"
Attempts:
2 left
💡 Hint

ConstructorParameters extracts the types of the constructor arguments as a tuple.

❓ Predict Output
intermediate
1:30remaining
What is the type of Params in this example?

Given this class, what is the type of Params?

Typescript
class Car {
  constructor(make: string, model: string, year: number) {}
}

type Params = ConstructorParameters<typeof Car>;
A[string, string, number]
Bstring[]
C[string, string]
D[string, number, string]
Attempts:
2 left
💡 Hint

Look at the constructor parameters order and types.

🔧 Debug
advanced
2:00remaining
Why does this code cause a TypeScript error?

Look at this code snippet. Why does it cause a TypeScript error?

Typescript
class Book {
  constructor(title: string, pages: number) {}
}

type Params = ConstructorParameters<typeof Book>;

const args: Params = ['My Book']; // Missing second argument
const b = new Book(...args);
ANo error, code runs fine
BError because ConstructorParameters cannot infer types from classes
CError because spread operator cannot be used with tuples
DError because args is missing the second parameter 'pages' required by the constructor
Attempts:
2 left
💡 Hint

Check if the tuple matches the constructor parameters exactly.

🧠 Conceptual
advanced
1:30remaining
What does ConstructorParameters return?

Choose the best description of what ConstructorParameters returns in TypeScript.

AThe return type of the constructor function of <code>SomeClass</code>
BA union type of all parameter types of the constructor of <code>SomeClass</code>
CA tuple type representing the parameter types of the constructor of <code>SomeClass</code> in order
DAn object type with keys as parameter names and values as their types
Attempts:
2 left
💡 Hint

Think about what a tuple is and how constructor parameters are represented.

❓ Predict Output
expert
2:30remaining
What is the output of this complex ConstructorParameters usage?

Analyze this TypeScript code and select the correct output.

Typescript
class Gadget {
  constructor(public id: number, public name: string, public active = true) {}
}

type Params = ConstructorParameters<typeof Gadget>;

function createGadget(...args: Params) {
  return new Gadget(...args);
}

const g1 = createGadget(101, 'Phone');
const g2 = createGadget(102, 'Tablet', false);

console.log(g1.active, g2.active);
Atrue false
Bundefined undefined
Ctrue true
Dfalse true
Attempts:
2 left
💡 Hint

Remember how default parameters work with tuples and spread arguments.

Practice

(1/5)
1.

What does the ConstructorParameters type utility do in TypeScript?

easy
A. Extracts the types of a class constructor's parameters as a tuple
B. Creates a new class with the same constructor parameters
C. Returns the return type of a class constructor
D. Converts constructor parameters into a union type

Solution

  1. Step 1: Understand the purpose of ConstructorParameters

    The ConstructorParameters utility extracts the types of the parameters used in a class constructor and returns them as a tuple type.
  2. Step 2: Compare with other options

    Options B, C, and D describe different behaviors that ConstructorParameters does not perform.
  3. Final Answer:

    Extracts the types of a class constructor's parameters as a tuple -> Option A
  4. Quick Check:

    ConstructorParameters = tuple of constructor args [OK]
Hint: Remember: ConstructorParameters gives constructor args as a tuple [OK]
Common Mistakes:
  • Confusing ConstructorParameters with ReturnType
  • Thinking it creates a new class
  • Assuming it returns a union type
2.

Which of the following is the correct syntax to get the constructor parameter types of a class MyClass using ConstructorParameters?

class MyClass { constructor(name: string, age: number) {} }
easy
A. type Params = ConstructorParameters<MyClass>;
B. type Params = ConstructorParameters;
C. type Params = ConstructorParameters;
D. type Params = ConstructorParameters;

Solution

  1. Step 1: Identify the correct type argument for ConstructorParameters

    The argument must be a constructor function type, which is typeof MyClass (the class constructor type), not the instance type.
  2. Step 2: Check each option

    type Params = ConstructorParameters<MyClass>; uses the instance type, which is incorrect. type Params = ConstructorParameters; uses a non-existent property. type Params = ConstructorParameters; uses invalid syntax. Only type Params = ConstructorParameters; correctly uses typeof MyClass.
  3. Final Answer:

    type Params = ConstructorParameters<typeof MyClass>; -> Option B
  4. Quick Check:

    Use typeof ClassName with ConstructorParameters [OK]
Hint: Use typeof ClassName to get constructor type [OK]
Common Mistakes:
  • Passing instance type instead of constructor type
  • Using invalid syntax like new expressions
  • Trying to access constructor property
3.

What is the output type of the following code?

class Person {
  constructor(public name: string, public age: number) {}
}

type Params = ConstructorParameters<typeof Person>;
medium
A. [string, string]
B. [name: string, age: number]
C. [string, number]
D. Error: ConstructorParameters cannot be applied

Solution

  1. Step 1: Analyze the constructor parameters

    The constructor takes two parameters: name of type string and age of type number.
  2. Step 2: Understand ConstructorParameters output

    ConstructorParameters<typeof Person> returns a tuple of the parameter types: [string, number].
  3. Final Answer:

    [string, number] -> Option C
  4. Quick Check:

    ConstructorParameters returns tuple of constructor arg types [OK]
Hint: ConstructorParameters returns tuple of constructor argument types [OK]
Common Mistakes:
  • Expecting parameter names in the tuple
  • Confusing parameter types with instance properties
  • Thinking it returns an error
4.

Identify the error in this code snippet:

class Car {
  constructor(make: string, year: number) {}
}

type Params = ConstructorParameters<Car>;
medium
A. Car is an instance type, should use typeof Car
B. ConstructorParameters cannot be used with classes
C. Constructor parameters must be public or private
D. Missing parentheses after Car in ConstructorParameters

Solution

  1. Step 1: Check the argument passed to ConstructorParameters

    The argument must be the constructor function type, which is typeof Car, not the instance type Car.
  2. Step 2: Identify the error

    Using Car directly causes a type error because it is the instance type, not the constructor type.
  3. Final Answer:

    Car is an instance type, should use typeof Car -> Option A
  4. Quick Check:

    Use typeof ClassName with ConstructorParameters [OK]
Hint: Always use typeof ClassName with ConstructorParameters [OK]
Common Mistakes:
  • Passing instance type instead of constructor type
  • Assuming ConstructorParameters works on instances
  • Adding parentheses after class name
5.

Given the following classes, how can you create a type alias EmployeeParams that matches the constructor parameters of Employee by reusing the constructor parameters of Person?

class Person {
  constructor(public name: string, public age: number) {}
}

class Employee extends Person {
  constructor(name: string, age: number, public salary: number) {
    super(name, age);
  }
}
hard
A. type EmployeeParams = ConstructorParameters<typeof Employee>;
B. type EmployeeParams = [string, number, number];
C. type EmployeeParams = ConstructorParameters<Person> & number;
D. type EmployeeParams = [...ConstructorParameters<typeof Person>, number];

Solution

  1. Step 1: Understand the constructor parameters

    Person constructor takes name and age. Employee adds salary as a third parameter.
  2. Step 2: Reuse Person's constructor parameters and add salary

    Using ConstructorParameters<typeof Person> gives [string, number]. Adding number for salary creates [string, number, number].
  3. Step 3: Check options

    type EmployeeParams = [...ConstructorParameters<typeof Person>, number]; correctly uses tuple spread to combine parameters. type EmployeeParams = ConstructorParameters<typeof Employee>; gets all parameters but does not reuse Person's. type EmployeeParams = ConstructorParameters<Person> & number; is invalid syntax. type EmployeeParams = [string, number, number]; hardcodes types without reuse.
  4. Final Answer:

    type EmployeeParams = [...ConstructorParameters<typeof Person>, number]; -> Option D
  5. Quick Check:

    Reuse constructor params with tuple spread [OK]
Hint: Use tuple spread to extend ConstructorParameters [OK]
Common Mistakes:
  • Not using typeof with class name
  • Hardcoding parameter types instead of reusing
  • Using invalid intersection (&) instead of tuple spread
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