Consider this TypeScript code using ConstructorParameters. What will be logged?
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);
ConstructorParameters extracts the types of the constructor arguments as a tuple.
The ConstructorParameters type extracts the parameter types of the constructor as a tuple. Here, Params is [string, number]. Passing args to the constructor creates a Person with name = 'Alice' and age = 30. So, logging p.name and p.age prints "Alice 30".
Given this class, what is the type of Params?
class Car { constructor(make: string, model: string, year: number) {} } type Params = ConstructorParameters<typeof Car>;
Look at the constructor parameters order and types.
The ConstructorParameters type extracts the constructor's parameter types as a tuple in the exact order. The constructor has parameters make: string, model: string, and year: number, so Params is [string, string, number].
Look at this code snippet. Why does it cause a TypeScript error?
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);
Check if the tuple matches the constructor parameters exactly.
The ConstructorParameters type expects a tuple with exactly two elements: string and number. The args array only has one element, so TypeScript raises an error because the second parameter is missing.
Choose the best description of what ConstructorParameters returns in TypeScript.
Think about what a tuple is and how constructor parameters are represented.
ConstructorParameters returns a tuple type representing the types of the constructor parameters in the exact order they appear. It does not return an object or union type, nor the return type of the constructor.
Analyze this TypeScript code and select the correct output.
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);
Remember how default parameters work with tuples and spread arguments.
The constructor has a default value for active as true. When calling createGadget(101, 'Phone'), the third parameter is omitted, so active defaults to true. For createGadget(102, 'Tablet', false), active is explicitly false. So the output is true false.