Course contentsShow
← TypeScript
Lesson 1089 of 1,79625. The this TypeFree lesson

ThisType vs Explicit this Parameters

Comparing ThisType<T> marker approach with explicit this parameters for different use cases.

ThisType vs Explicit this Parameters

You'll learn the key differences between using ThisType<T> and explicit this parameters, and when each approach is the right tool for the job.


Two Ways to Type this

TypeScript gives you two distinct mechanisms to control the type of this inside functions and methods:

  1. Explicit this parameters: A fake first parameter that declares what this should be
  2. ThisType<T> marker: A special utility type that injects this context into object literals

Think of explicit this parameters as direct instruction—you tell each function exactly what this is. ThisType<T> is more like ambient instruction—you set the context once for an entire object, and TypeScript applies it to all methods inside.


Explicit this Parameters: Direct Control

When you add this as the first parameter, you're being explicit about context on each function:

interface Person {
  name: string;
  greet(this: Person): void;
}

const person: Person = {
  name: "Alice",
  greet(this: Person) {
    console.log(`Hello, ${this.name}`);
  }
};

Use when:

  • You need precise, per-method control
  • The function might be called in different contexts
  • You want clear documentation at the function signature
  • You're typing standalone functions or callbacks

ThisType<T>: Context for Whole Objects

ThisType<T> doesn't add properties—it tells TypeScript "treat this as type T inside all methods of this object":

type Methods<T> = {
  greet(): void;
  describe(): string;
} & ThisType<T & Methods<T>>;

function createPerson<T>(data: T, methods: Methods<T>) {
  return Object.assign({}, data, methods);
}

const person = createPerson(
  { name: "Bob", age: 30 },
  {
    greet() {
      // this is typed as { name: string; age: number } & Methods
      console.log(this.name);
    },
    describe() {
      return `${this.name} is ${this.age}`;
    }
  }
);

Use when:

  • You're building factory functions or frameworks
  • You want to avoid repetitive this parameters
  • You're creating plugin systems or mixin patterns
  • The this context is complex and shared across many methods

The Key Trade-offs

Explicit this parameters are:

  • ✅ More visible and self-documenting
  • ✅ Work for individual functions
  • ✅ Easier to understand for beginners
  • ❌ Repetitive when you have many methods

ThisType<T> is:

  • ✅ Cleaner for object literals with many methods
  • ✅ Perfect for factory/builder patterns
  • ✅ Enables complex type composition
  • ❌ Less obvious what's happening
  • ❌ Only works in object literal contexts

Choosing the Right Tool

Use explicit this parameters for callbacks, standalone functions, or when each method needs different context:

button.addEventListener('click', function(this: HTMLButtonElement) {
  this.disabled = true; // this is clearly HTMLButtonElement
});

Use ThisType<T> when building APIs where users provide method objects and you control the final context:

// Framework code providing rich this context
type PluginMethods = {
  init(): void;
  destroy(): void;
} & ThisType<{ config: Config; utils: Utils }>;

Key Takeaway: Use explicit this parameters for clear, per-function typing; use ThisType<T> when building frameworks or factories where you need to inject shared context into many methods at once.