Generic arrow functions let you write flexible functions that work with many types without repeating code.
Generic arrow functions in Typescript
const functionName = <T>(param: T): T => { // function body return param; };
The <T> declares a generic type parameter named T.
You can use any letter or name instead of T, but T is common.
const identity = <T>(value: T): T => value;const wrapInArray = <T>(item: T): T[] => [item];const getFirst = <T>(arr: T[]): T | undefined => arr[0];
This program shows a generic arrow function echo that returns the input as is. It works with both numbers and strings.
const echo = <T>(input: T): T => input; const numberEcho = echo(123); const stringEcho = echo("hello"); console.log(numberEcho); console.log(stringEcho);
Generic functions help keep your code DRY (Don't Repeat Yourself).
TypeScript checks that you use the same type for input and output when using generics.
Generic arrow functions let you write one function for many types.
Use <T> to declare a generic type parameter.
This keeps your code flexible and type-safe.
Practice
What does the <T> represent in a generic arrow function in TypeScript?
Solution
Step 1: Understand generic type parameters
The<T>syntax declares a generic type parameter, which acts as a placeholder for any type.Step 2: Recognize its purpose in functions
This allows the function to work with different types while keeping type safety.Final Answer:
A placeholder for a type that will be specified later -> Option BQuick Check:
Generic type parameter = placeholder type [OK]
- Confusing <T> with a variable name
- Thinking <T> is a keyword like const or let
- Assuming <T> is an operator
Which of the following is the correct syntax for a generic arrow function that returns the same type it receives?
?Solution
Step 1: Check generic arrow function syntax
The generic type parameter<T>must come immediately after the function name without an equals sign.Step 2: Verify arrow function structure
The function takes an argument of typeTand returns the same typeT.const identity = <T>(arg: T): T => arg;matches this syntax correctly.Final Answer:
const identity = <T>(arg: T): T => arg; -> Option CQuick Check:
Correct generic arrow function syntax =const identity = <T>(arg: T): T => arg;[OK]
- Adding an equals sign after <T>
- Misplacing parentheses around <T>
- Using incorrect arrow function syntax
What will be the output of this TypeScript code?
const wrapInArray = <T>(value: T): T[] => [value];
console.log(wrapInArray(5));
console.log(wrapInArray('hello'));Solution
Step 1: Understand the generic function behavior
The functionwrapInArraytakes a value of any typeTand returns an array containing that value.Step 2: Analyze the console.log outputs
CallingwrapInArray(5)returns[5], andwrapInArray('hello')returns['hello'].Final Answer:
[5] ['hello'] -> Option AQuick Check:
Generic function returns array with input value [OK]
- Expecting output without array brackets
- Thinking it returns a tuple
- Confusing types causing errors
Identify the error in this generic arrow function and choose the corrected version:
const getFirst = <T> = (arr: T[]): T => {
return arr[0];
};Solution
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.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.Final Answer:
const getFirst = <T>(arr: T[]): T => arr[0]; -> Option DQuick Check:
Generic arrow function syntax =const getFirst = <T>(arr: T[]): T => arr[0];[OK]
- Adding equals sign after <T>
- Omitting generic parameter <T>
- Incorrect arrow function syntax
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}));Solution
Step 1: Understand the generic parameters and return type
The function takes two objects of typesTandUand returns their intersection typeT & U, meaning it combines properties from both.Step 2: Analyze the spread operator usage
The spread operator merges properties from both objects into a new object containing all keys and values.Step 3: Predict the output of the console.log
CallingmergeObjects({a: 1}, {b: 2})returns{ a: 1, b: 2 }.Final Answer:
{ a: 1, b: 2 } -> Option AQuick Check:
Spread merge of two objects = combined object [OK]
- Expecting only first or second object properties
- Confusing intersection type with union
- Thinking it causes runtime error
Ready to prove it?
Pass the test and this pattern moves unknown
