Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same letter for both generic parameters.
Using lowercase letters for generic parameters.
✗ Incorrect
The first generic type parameter is usually named T. Here, T and U are used as generic parameters.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both generic parameters.
Using lowercase letters.
✗ Incorrect
The first generic parameter is named T, which is commonly used for generic types.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for generic parameters.
Using the same letter for both parameters.
✗ Incorrect
Generic type parameters must be uppercase letters. 'T' is correct, 't' is invalid.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same letter for both generic parameters.
Using lowercase letters.
✗ Incorrect
The generic class uses T and U as type parameters for the two types.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for generic parameters.
Using the same name for generic parameters and function parameters.
✗ Incorrect
The generic parameters are K and V for key and value types, and the key parameter is named 'key'.