Course contentsShow
← TypeScript
Lesson 1739 of 1,79639. TypeScript PerformanceFree lesson

Use Type Parameters Instead of Lookups

Pass types as generic parameters rather than using indexed access when the type is already known.

Use Type Parameters Instead of Lookups

You'll learn: When to pass types directly as generic parameters rather than extracting them via indexed access for better TypeScript performance.

What You're Optimizing

By this point in your TypeScript journey, you've seen patterns like this countless times:

function processUser<T extends User>(user: T) {
  const name: T['name'] = user.name;
  const email: T['email'] = user.email;
  return { name, email };
}

Those T['name'] and T['email'] lookups work perfectly fine. But they come with a hidden cost: every indexed access creates work for TypeScript's type checker. It must resolve the property, check constraints, and cache the result.

The Performance Alternative

When you already know the type you need, pass it as a separate generic parameter instead of looking it up:

// Slower: lookup required
function createPair<T extends { value: string }>(obj: T): [T, T['value']] {
  return [obj, obj.value];
}

// Faster: type passed directly
function createPair<T extends { value: V }, V extends string>(
  obj: T
): [T, V] {
  return [obj, obj.value];
}

The second version eliminates the T['value'] lookup. TypeScript doesn't need to dig into T's structure—the type V is already provided.

Think of It Like Function Arguments

Imagine you have a function that needs a person's age. You could:

  • Lookup approach: Pass the entire person object and extract person.age inside
  • Parameter approach: Pass the age directly as a separate parameter

The parameter approach is more efficient—no searching required. The same logic applies to type parameters.

When This Matters

This optimization shines in:

  • High-frequency generic utilities called throughout your codebase
  • Complex mapped types where lookups compound
  • Library code where performance affects all consumers
  • Large codebases where compile times are already slow

For small projects or one-off functions, the readability of indexed access often wins.

Key Takeaway: When a type is already available in scope, prefer passing it as a generic parameter over extracting it via indexed access—TypeScript's checker will thank you with faster compile times.