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

Instantiation vs Declaration

The difference between type declaration analysis and type instantiation costs.

Instantiation vs Declaration

You'll learn: The performance difference between defining a type and creating concrete versions of it with specific arguments.

When working with TypeScript's type system, the compiler distinguishes between two phases: declaring a type and instantiating it. Understanding this difference is crucial for performance optimization.

What Is Declaration?

When you define a generic type, interface, or type alias, TypeScript analyzes its structure once. This is declaration. The compiler validates the syntax, checks constraints, and stores the type definition. This happens once per type definition, regardless of how many times you use it.

Think of declaration as writing a recipe. You write it once, validate the steps make sense, and put it in a cookbook.

What Is Instantiation?

When you use a generic type with specific type arguments, TypeScript creates a concrete version of that type. This is instantiation. Each unique combination of type arguments creates a new instantiation.

Instantiation is like following the recipe with specific ingredients. Every time you use different ingredients (type arguments), you're executing the recipe again.

The Performance Impact

Declaration is cheap—it happens once. Instantiation can be expensive when:

  • You instantiate the same generic type with many different arguments
  • The generic type contains complex conditional logic or recursion
  • Type argument combinations create deeply nested structures

Practical Example Without Code

Imagine a generic Result<T> type. Declaring it once is fast. But if your codebase instantiates Result<User>, Result<Post>, Result<Comment>, and hundreds more, each instantiation requires type-checking work.

Deeply nested generics compound this cost. A type like DeepPartial<ComplexObject> with recursive logic must be instantiated fresh for each unique structure it processes.

Key Takeaway: Declaration analysis is fast and happens once; instantiation happens repeatedly and can become a performance bottleneck when generic types are complex or used with many different type arguments.