0
0
Typescriptprogramming~10 mins

Multiple generic parameters 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 declare a generic function with two type parameters.

Typescript
function pair<[1], U>(first: [1], second: U): [[1], U] {
  return [first, second];
}
Drag options to blanks, or click blank then click option'
AU
BT
CV
DS
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same letter for both generic parameters.
Using lowercase letters for generic parameters.
2fill in blank
medium

Complete the code to create a generic interface with two type parameters.

Typescript
interface Result<[1], U> {
  success: boolean;
  data: [1] | U;
}
Drag options to blanks, or click blank then click option'
AT
BR
CS
DU
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both generic parameters.
Using lowercase letters.
3fill in blank
hard

Fix the error in the generic function declaration with two parameters.

Typescript
function merge<[1], U>(obj1: [1], obj2: U): [1] & U {
  return { ...obj1, ...obj2 };
}
Drag options to blanks, or click blank then click option'
AU
Bt
CT
DV
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for generic parameters.
Using the same letter for both parameters.
4fill in blank
hard

Fill both blanks to declare a generic class with two type parameters and a method using them.

Typescript
class Pair<[1], [2]> {
  constructor(public first: [1], public second: [2]) {}
  getPair(): [[1], [2]] {
    return [this.first, this.second];
  }
}
Drag options to blanks, or click blank then click option'
AT
BU
CV
DS
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same letter for both generic parameters.
Using lowercase letters.
5fill in blank
hard

Fill all three blanks to create a generic function that takes two parameters and returns an object with keys and values of those types.

Typescript
function createMap<[1], [2]>(key: [1], value: [2]): { [1]: [2] } {
  return { [key]: value };
}
Drag options to blanks, or click blank then click option'
AK
BV
Ckey
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for generic parameters.
Using the same name for generic parameters and function parameters.