0
0
Typescriptprogramming~5 mins

ConstructorParameters type in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
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
If a class constructor has no parameters, what does ConstructorParameters return?
AAn empty tuple []
Bundefined
Cnever
Dnull
Which TypeScript utility type would you use to get the types of constructor parameters?
AConstructorParameters
BParameters
CReturnType
DInstanceType
What is the type of ConstructorParameters?
A[number, number, number, number, number, number, number]
B[]
C[string]
D[string | number | undefined, ...any[]]
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.