Q59: How do you explicitly type the `this` context inside functions?

Difficulty:MediumTopic:Functions

You can declare a fake parameter named this as the first parameter of a function signature. TypeScript strips this this parameter during compilation.

Explicit this Parameter Context
interface ButtonElement {
  addClickListener(onclick: (this: ButtonElement, e: Event) => void): void;
}
💡 Declaring this as the first parameter sets the expected execution context for the callback.
Key Takeaways
  • Explicit this parameters type-check execution context inside callback functions.