0
0
Typescriptprogramming~10 mins

ConstructorParameters type in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract constructor parameter types using ConstructorParameters.

Typescript
type Params = [1]<Date>;
Drag options to blanks, or click blank then click option'
AReturnType
BParameters
CConstructorParameters
DInstanceType
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parameters instead of ConstructorParameters
Using ReturnType which extracts return types
Using InstanceType which extracts instance types
2fill in blank
medium

Complete the code to define a class and extract its constructor parameter types.

Typescript
class Person {
  constructor(public name: string, public age: number) {}
}
type PersonParams = [1]<typeof Person>;
Drag options to blanks, or click blank then click option'
AConstructorParameters
BParameters
CReturnType
DInstanceType
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parameters which works with functions, not constructors
Using InstanceType which gives the instance type, not parameters
3fill in blank
hard

Fix the error in the code to correctly extract constructor parameters of a class.

Typescript
class Car {
  constructor(make: string, year: number) {}
}
type CarParams = ConstructorParameters<[1]>;
Drag options to blanks, or click blank then click option'
Atypeof Car
BCar
CCarConstructor
DInstanceType<Car>
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name directly instead of typeof
Using InstanceType which is for instances, not constructors
4fill in blank
hard

Fill both blanks to create a function that accepts constructor parameters of a class.

Typescript
function createInstance(cls: [1], ...args: [2]) {
  return new cls(...args);
}
Drag options to blanks, or click blank then click option'
Anew (...args: any) => any
BConstructorParameters<typeof cls>
Dany[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance type instead of constructor signature
Not using ConstructorParameters for the args
5fill in blank
hard

Fill all three blanks to define a generic function that creates instances using constructor parameters.

Typescript
function makeInstance<T extends [1]>(cls: T, ...args: [2]<T>): InstanceType<T> {
  return new cls(...args);
}

type Params<T> = [3]<T>;
Drag options to blanks, or click blank then click option'
Anew (...args: any) => any
BConstructorParameters
DParameters
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parameters instead of ConstructorParameters
Not constraining T to a constructor signature