Course contentsShow
← TypeScript
Lesson 283 of 1,7966. FunctionsFree lesson

this: void for No-Context Functions

Using this: void to indicate a function should not be called with a this context.

this: void for No-Context Functions

What you'll learn: How to explicitly declare that a function should never be called with a this context.

The Problem: Functions That Don't Need this

Sometimes you write functions that are meant to be called as standalone functions, not as methods. These functions don't use this at all and shouldn't be called with any this context.

Using this: void as Documentation

You can declare this: void as the first parameter to signal that a function expects no this context:

function standaloneLogger(this: void, message: string): void {
  console.log(message);
}

standaloneLogger("Hello"); // ✓ OK

This is purely a compile-time check — it doesn't appear in the compiled JavaScript. It's documentation that TypeScript enforces.

When TypeScript Catches Misuse

If someone tries to use your this: void function as a method or with .call(), TypeScript will raise an error:

function standaloneLogger(this: void, message: string): void {
  console.log(message);
}

const obj = {
  log: standaloneLogger
};

obj.log("test"); // ✗ Error: The 'this' context of type '{ log: (message: string) => void; }' 
                 // is not assignable to method's 'this' of type 'void'

When to Use It

Use this: void when:

  • Writing utility functions that should never access this
  • Preventing accidental method-style usage
  • Making your intent clear to other developers and TypeScript

Think of it as a "no context allowed" sign on your function.

Key Takeaway: this: void explicitly documents and enforces that a function should be called without any this context, preventing misuse as a method.