Bird
Raised Fist0
Typescriptprogramming~20 mins

Generic arrow functions in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Generic Arrow Functions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a generic arrow function with array input
What is the output of this TypeScript code using a generic arrow function that returns the first element of an array?
Typescript
const firstElement = <T>(arr: T[]): T => arr[0];

console.log(firstElement([10, 20, 30]));
AError at compile time
B20
C10
Dundefined
Attempts:
2 left
💡 Hint
The function returns the first item of the array passed in.
❓ Predict Output
intermediate
2:00remaining
Output of generic arrow function with object type
What will this TypeScript code output when using a generic arrow function to get a property value from an object?
Typescript
const getProp = <T, K extends keyof T>(obj: T, key: K): T[K] => obj[key];

const person = { name: 'Alice', age: 30 };
console.log(getProp(person, 'name'));
A"Alice"
B30
Cundefined
DType error at compile time
Attempts:
2 left
💡 Hint
The function returns the value of the given key from the object.
🔧 Debug
advanced
2:00remaining
Identify the error in this generic arrow function
This TypeScript generic arrow function is intended to swap two elements in a tuple. What error will it produce?
Typescript
const swap = <T, U>(pair: [T, U]): [U, T] => {
  return [pair[1], pair[0]];
};

const result = swap([true, 42]);
console.log(result);
AType error: Cannot infer type parameters
BSyntax error: missing return type annotation
CRuntime error: undefined is not iterable
DNo error, outputs [42, true]
Attempts:
2 left
💡 Hint
Check if the function correctly swaps tuple elements and returns the expected type.
📝 Syntax
advanced
2:00remaining
Which option has correct syntax for a generic arrow function?
Which of the following is the correct syntax for a generic arrow function in TypeScript that returns the length of an array?
Aconst length = (arr: T[]) => arr.length;
Bconst length = <T>(arr: T[]) => arr.length;
Cconst length = <T>(arr: Array) => arr.length;
Dconst length = <T>(arr: T[]) => { arr.length }
Attempts:
2 left
💡 Hint
Generic type parameters must be declared before the function parameters.
🚀 Application
expert
3:00remaining
Using generic arrow function to merge two objects
Given this generic arrow function to merge two objects, what is the type of the returned object when merging {a: 1} and {b: 'x'}?
Typescript
const merge = <T, U>(obj1: T, obj2: U): T & U => ({ ...obj1, ...obj2 });

const merged = merge({ a: 1 }, { b: 'x' });
A{ a: number; b: string; }
B{ b: string; }
C{ a: number; }
DError: Cannot merge objects of different types
Attempts:
2 left
💡 Hint
The returned type combines properties from both input objects.

Practice

(1/5)
1.

What does the <T> represent in a generic arrow function in TypeScript?

easy
A. A variable name for a function parameter
B. A placeholder for a type that will be specified later
C. A keyword to declare a constant
D. A special operator for comparison

Solution

  1. Step 1: Understand generic type parameters

    The <T> syntax declares a generic type parameter, which acts as a placeholder for any type.
  2. Step 2: Recognize its purpose in functions

    This allows the function to work with different types while keeping type safety.
  3. Final Answer:

    A placeholder for a type that will be specified later -> Option B
  4. Quick Check:

    Generic type parameter = placeholder type [OK]
Hint: Think of <T> as a flexible type placeholder [OK]
Common Mistakes:
  • Confusing <T> with a variable name
  • Thinking <T> is a keyword like const or let
  • Assuming <T> is an operator
2.

Which of the following is the correct syntax for a generic arrow function that returns the same type it receives?

?
easy
A. const identity = <T> = (arg: T): T => arg;
B. const identity = <T>: (arg: T) => T => arg;
C. const identity = <T>(arg: T): T => arg;
D. const identity = <T> (arg: T) => { return arg; };

Solution

  1. Step 1: Check generic arrow function syntax

    The generic type parameter <T> must come immediately after the function name without an equals sign.
  2. Step 2: Verify arrow function structure

    The function takes an argument of type T and returns the same type T. const identity = <T>(arg: T): T => arg; matches this syntax correctly.
  3. Final Answer:

    const identity = <T>(arg: T): T => arg; -> Option C
  4. Quick Check:

    Correct generic arrow function syntax = const identity = <T>(arg: T): T => arg; [OK]
Hint: Generic syntax: const fn = <T>(arg: T): T => ... [OK]
Common Mistakes:
  • Adding an equals sign after <T>
  • Misplacing parentheses around <T>
  • Using incorrect arrow function syntax
3.

What will be the output of this TypeScript code?

const wrapInArray = <T>(value: T): T[] => [value];

console.log(wrapInArray(5));
console.log(wrapInArray('hello'));
medium
A. [5] ['hello']
B. 5 hello
C. [5, 'hello']
D. Error: Type mismatch

Solution

  1. Step 1: Understand the generic function behavior

    The function wrapInArray takes a value of any type T and returns an array containing that value.
  2. Step 2: Analyze the console.log outputs

    Calling wrapInArray(5) returns [5], and wrapInArray('hello') returns ['hello'].
  3. Final Answer:

    [5] ['hello'] -> Option A
  4. Quick Check:

    Generic function returns array with input value [OK]
Hint: Generic function wraps input in array, output matches input type [OK]
Common Mistakes:
  • Expecting output without array brackets
  • Thinking it returns a tuple
  • Confusing types causing errors
4.

Identify the error in this generic arrow function and choose the corrected version:

const getFirst = <T> = (arr: T[]): T => {
  return arr[0];
};
medium
A. const getFirst = <T> (arr: T[]): T => { return arr[0]; };
B. const getFirst = (arr: T[]): T => arr[0];
C. const getFirst = <T>: (arr: T[]) => T => arr[0];
D. const getFirst = <T>(arr: T[]): T => arr[0];

Solution

  1. Step 1: Identify incorrect syntax in original code

    The original code incorrectly uses an equals sign after <T>, which is not valid syntax for generic arrow functions.
  2. Step 2: Choose the correct syntax

    const getFirst = <T>(arr: T[]): T => arr[0]; correctly places <T> immediately after the function name without an equals sign and uses concise arrow function syntax.
  3. Final Answer:

    const getFirst = <T>(arr: T[]): T => arr[0]; -> Option D
  4. Quick Check:

    Generic arrow function syntax = const getFirst = <T>(arr: T[]): T => arr[0]; [OK]
Hint: No equals sign after <T> in generic arrow functions [OK]
Common Mistakes:
  • Adding equals sign after <T>
  • Omitting generic parameter <T>
  • Incorrect arrow function syntax
5.

Given this generic arrow function, what is the output of mergeObjects({a: 1}, {b: 2})?

const mergeObjects = <T, U>(obj1: T, obj2: U): T & U => ({ ...obj1, ...obj2 });

console.log(mergeObjects({a: 1}, {b: 2}));
hard
A. { a: 1, b: 2 }
B. { a: 1 }
C. { b: 2 }
D. TypeError at runtime

Solution

  1. Step 1: Understand the generic parameters and return type

    The function takes two objects of types T and U and returns their intersection type T & U, meaning it combines properties from both.
  2. Step 2: Analyze the spread operator usage

    The spread operator merges properties from both objects into a new object containing all keys and values.
  3. Step 3: Predict the output of the console.log

    Calling mergeObjects({a: 1}, {b: 2}) returns { a: 1, b: 2 }.
  4. Final Answer:

    { a: 1, b: 2 } -> Option A
  5. Quick Check:

    Spread merge of two objects = combined object [OK]
Hint: Spread operator merges objects, output has all keys [OK]
Common Mistakes:
  • Expecting only first or second object properties
  • Confusing intersection type with union
  • Thinking it causes runtime error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard