Recall & Review
beginner
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?✗ Incorrect
ConstructorParameters extracts the types of the constructor parameters as a tuple.
Can
ConstructorParameters be used on a regular function?✗ Incorrect
ConstructorParameters only works with class constructors or constructor signatures.
If a class constructor has no parameters, what does
ConstructorParameters return?✗ 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?
✗ Incorrect
ConstructorParameters is designed to extract constructor parameter types.
What is the type of
ConstructorParameters?✗ Incorrect
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.