What does the ConstructorParameters type utility do in TypeScript?
It extracts the types of the parameters of a class constructor as a tuple type. This helps you know what arguments a class constructor expects.
Click to reveal answer
beginner
How do you use ConstructorParameters with a class MyClass?
You write ConstructorParameters<typeof MyClass>. This gives you a tuple type of the constructor's parameter types.
Click to reveal answer
beginner
Given <code>class Car { constructor(make: string, year: number) {} }</code>, what is <code>ConstructorParameters<typeof Car></code>?
It is the tuple type [string, number], representing the types of the constructor parameters in order.
Click to reveal answer
intermediate
Can ConstructorParameters be used with functions?
No, it only works with class constructors or constructor signatures, not regular functions.
Click to reveal answer
intermediate
Why is ConstructorParameters useful in real code?
It helps you create types or functions that need to know what arguments to pass when creating instances of classes, improving type safety and reducing errors.
Click to reveal answer
What does ConstructorParameters return?
AThe return type of the constructor
BA tuple type of the constructor's parameter types
CThe instance type of the class
DThe names of the constructor parameters
✗ Incorrect
ConstructorParameters extracts the types of the constructor parameters as a tuple.
Can ConstructorParameters be used on a regular function?
AYes, it works on any function
BNo, only on interfaces
CNo, only on class constructors
DYes, but only on arrow functions
✗ Incorrect
ConstructorParameters only works with class constructors or constructor signatures.
If a class constructor has no parameters, what does ConstructorParameters return?
AAn empty tuple []
Bundefined
Cnever
Dnull
✗ Incorrect
It returns an empty tuple type because there are no parameters.
Which TypeScript utility type would you use to get the types of constructor parameters?
AConstructorParameters
BParameters
CReturnType
DInstanceType
✗ Incorrect
ConstructorParameters is designed to extract constructor parameter types.
The Date constructor accepts multiple optional parameters of type string or number, so the tuple reflects that.
Explain what the ConstructorParameters type utility does and give an example.
Think about how to get the types of arguments a class constructor takes.
You got /3 concepts.
Describe a situation where using ConstructorParameters improves your TypeScript code.
Consider when you want to create instances dynamically with correct argument types.
You got /4 concepts.
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
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.
Step 2: Compare with other options
Options B, C, and D describe different behaviors that ConstructorParameters does not perform.
Final Answer:
Extracts the types of a class constructor's parameters as a tuple -> Option A
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
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.
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.
Final Answer:
type Params = ConstructorParameters<typeof MyClass>; -> Option B
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
Step 1: Analyze the constructor parameters
The constructor takes two parameters: name of type string and age of type number.
Step 2: Understand ConstructorParameters output
ConstructorParameters<typeof Person> returns a tuple of the parameter types: [string, number].
Final Answer:
[string, number] -> Option C
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
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.
Step 2: Identify the error
Using Car directly causes a type error because it is the instance type, not the constructor type.
Final Answer:
Car is an instance type, should use typeof Car -> Option A
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
Step 1: Understand the constructor parameters
Person constructor takes name and age. Employee adds salary as a third parameter.
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].
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.
Final Answer:
type EmployeeParams = [...ConstructorParameters<typeof Person>, number]; -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.