Advanced generics help you write flexible and safe code that works with many types without repeating yourself.
Why advanced generics matter in Typescript
Introduction
Syntax
Typescript
function example<T, U>(arg1: T, arg2: U): [T, U] { return [arg1, arg2]; }
T and U are type placeholders called generics.
You can use multiple generics separated by commas inside angle brackets <>.
Examples
Typescript
function identity<T>(value: T): T { return value; }
Typescript
function merge<T, U>(obj1: T, obj2: U): T & U { return { ...obj1, ...obj2 }; }
Typescript
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; }
Sample Program
This program shows how one generic function can wrap any value in an array, keeping the type correct.
Typescript
function wrapInArray<T>(value: T): T[] { return [value]; } const numberArray = wrapInArray(5); const stringArray = wrapInArray('hello'); console.log(numberArray); console.log(stringArray);
Important Notes
Advanced generics let TypeScript catch mistakes before running your code.
They make your code reusable and easier to read by showing clear type relationships.
Start simple, then add more generic details as you need more flexibility.
Summary
Advanced generics let you write flexible, reusable, and type-safe code.
They help avoid bugs by checking types automatically.
Using generics well makes your code easier to maintain and understand.
Practice
1. What is the main benefit of using advanced generics in TypeScript?
easy
Solution
Step 1: Understand what generics do
Generics let you write code that works with many types without losing type safety.Step 2: Identify the main benefit
Advanced generics improve flexibility and reusability while preventing type errors.Final Answer:
They allow writing flexible and reusable code with type safety. -> Option CQuick Check:
Advanced generics = flexible reusable type-safe code [OK]
Hint: Think: generics = flexible + safe code [OK]
Common Mistakes:
- Confusing generics with runtime performance improvements
- Believing generics fix bugs automatically
- Thinking generics replace functions
2. Which of the following is the correct syntax to declare a generic function in TypeScript?
easy
Solution
Step 1: Check generic syntax
Generic functions declare a type parameter inside angle brackets after the function name.Step 2: Verify parameter and return types
The argument and return type should use the generic type parameter T.Final Answer:
function example<T>(arg: T): T { return arg; } -> Option DQuick Check:
Generic function syntax = function name<T>(arg: T): T [OK]
Hint: Generic syntax: function name<T>(arg: T): T [OK]
Common Mistakes:
- Omitting the generic parameter declaration <T>
- Not typing the argument with T
- Returning a value in a void function
3. What will be the output of this TypeScript code?
function wrapInArray<T>(value: T): T[] {
return [value];
}
const result = wrapInArray(42);
console.log(result);medium
Solution
Step 1: Understand the function behavior
The function takes a value of any type T and returns an array containing that value.Step 2: Apply the function to the input
Calling wrapInArray(42) returns [42], an array with the number 42 inside.Final Answer:
[42] -> Option AQuick Check:
wrapInArray(42) = [42] [OK]
Hint: Generic function returns array with input inside [OK]
Common Mistakes:
- Expecting just the value instead of an array
- Thinking it returns undefined
- Confusing type errors with runtime output
4. Identify the error in this TypeScript generic function:
function identity<T>(arg: T): T {
return arg.length;
}medium
Solution
Step 1: Analyze the return statement
The function returns arg.length, which is a number.Step 2: Check return type consistency
The function declares it returns type T, but number may not be T, causing a type mismatch.Final Answer:
The function returns a number but declares return type T. -> Option AQuick Check:
Return type mismatch = error [OK]
Hint: Return type must match declared generic type [OK]
Common Mistakes:
- Assuming arg always has length property
- Ignoring return type mismatch
- Thinking generic declaration is wrong
5. Given this generic interface and function, what is the correct way to use advanced generics to ensure type safety?
interface Container<T> {
value: T;
}
function getValue<T>(container: Container<T>): T {
return container.value;
}
const numberContainer = { value: 123 };
const stringContainer = { value: "hello" };
// Which call is correct?hard
Solution
Step 1: Match generic type with container value
numberContainer has value of type number, so T should be number.Step 2: Check each option for type safety
getValue<number>(numberContainer); uses getValue<number>(numberContainer), which matches types correctly.Final Answer:
getValue<number>(numberContainer); -> Option BQuick Check:
Generic type matches container value type [OK]
Hint: Generic type must match container's value type [OK]
Common Mistakes:
- Passing wrong generic type for container value
- Omitting generic type when explicit needed
- Mixing container types incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
