Course contentsShow
← TypeScript
Lesson 549 of 1,79613. GenericsFree lesson

When NOT to Use Generic Functions

Recognizing situations where concrete types, overloads, or union types are clearer than generic parameters.

When NOT to Use Generic Functions

You'll learn: How to recognize when generics make your code worse instead of better, and what to use instead.

The Core Idea

Generics are powerful, but they're not always the right tool. Sometimes a concrete type, a simple union, or even function overloads will make your code clearer and safer. Using a generic "just because you can" often creates complexity without benefit.

Think of generics like a Swiss Army knife — fantastic when you need multiple tools in one, but if you only need to cut an apple, just use a regular knife. Reaching for generics when simpler types work is like using the entire Swiss Army knife to spread butter.

When to Skip Generics

The Type Never Changes

If your function always returns the same type regardless of input, don't use a generic:

// BAD: Generic adds no value
function getLength<T>(arr: T[]): number {
  return arr.length;
}

// GOOD: Concrete type is clearer
function getLength(arr: unknown[]): number {
  return arr.length;
}

You Need a Specific Set of Types

When only 2-3 specific types are valid, use a union or overloads instead:

// BAD: Generic is too loose
function format<T>(value: T): string {
  return String(value); // Works with anything, not helpful
}

// GOOD: Union shows exactly what's allowed
function format(value: string | number | boolean): string {
  return String(value);
}

The Relationship Is Too Simple

If the return type doesn't actually depend on the input type in a meaningful way, skip the generic. Generics shine when there's a preservation relationship — otherwise, they just add noise.

Key Takeaway: Use generics when you need to preserve type relationships. If a union, concrete type, or overload is clearer and equally safe, choose simplicity over generality.