0
0
Typescriptprogramming~20 mins

ConstructorParameters type in Typescript - Practice Problems & Coding Challenges

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