0
0
Typescriptprogramming~10 mins

Generic arrow functions 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 arrow function that returns the input value.

Typescript
const identity = <T>([1]: T): T => [1];
Drag options to blanks, or click blank then click option'
Ainput
Barg
Citem
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and return value.
Forgetting to use the generic type .
2fill in blank
medium

Complete the code to declare a generic arrow function that returns the length of an array.

Typescript
const getLength = <T>(arr: T[]): number => arr[1];
Drag options to blanks, or click blank then click option'
A.size
B.length()
C.length
D.count
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses after length like a function.
Using incorrect property names like size or count.
3fill in blank
hard

Fix the error in the generic arrow function that swaps two elements in a tuple.

Typescript
const swap = <T, U>(pair: [T, U]): [U, T] => [pair[1]1, pair[0]];
Drag options to blanks, or click blank then click option'
A[
B{
C(
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation before the index like pair.1 which is invalid.
Using parentheses or braces instead of square brackets.
4fill in blank
hard

Complete the code to create a generic arrow function that returns the first element of an array.

Typescript
const firstElement = <T>(arr: T]): T | undefined => arr[[1];
Drag options to blanks, or click blank then click option'
A[
B0
C1
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using index 1 instead of 0.
5fill in blank
hard

Fill all three blanks to create a generic arrow function that filters an array by a condition function.

Typescript
const filterArray = <T>(arr: T[], condition: (item: T) => boolean): T[] => arr.filter([1] => [2]([3]));
Drag options to blanks, or click blank then click option'
Aitem
Ccondition(item)
Dcondition
Attempts:
3 left
💡 Hint
Common Mistakes
Using different parameter names inside and outside the arrow function.
Not calling the condition function properly.