ReviseAlgo Logo

Functions in TypeScript

Typing this in Functions

Master declaring fake 'this' parameters in TypeScript functions, enforcing method invocation context, and resolving noImplicitThis compiler errors.

Last Updated: July 29, 2026 10 min read

In JavaScript, this is dynamically bound at call-site. TypeScript allows developers to explicitly type this inside standard function declarations using a fake this parameter.

1. Fake this Parameter Syntax

To type this inside a standalone or object function, add a parameter named this as the first parameter of the function declaration:

Important: The this parameter is fake. It is completely erased by tsc during compilation and does not count as an actual function argument at runtime!

2. Preventing Method Detachment Errors

Explicitly typing this catches detached method calls at compile time:

3. Typing this in Event Handlers (DOM Events)

DOM event listeners frequently bind this to the HTML element that dispatched the event:

4. noImplicitThis Compiler Flag

When "noImplicitThis": true is enabled in tsconfig.json, TypeScript will raise a compile error whenever this is used inside a function without an explicit annotation or inferred context.

5. Interactive Code Playground

Test this annotations below:

6. Common Pitfalls & Edge Cases

  • Arrow Functions Do NOT Accept Fake this Parameters: Arrow functions inherit this lexically from their enclosing scope. Placing a fake this parameter in an arrow function signature is a syntax error.
  • Passing Callbacks to Third-Party Libraries: Ensure third-party callback interfaces match your function's this requirement.
  • 7. Summary Checklist

  • [x] Use a fake this parameter as the first parameter of standard functions to type this.
  • [x] Fake this parameters are completely erased during JavaScript compilation (tsc).
  • [x] Enable "noImplicitThis": true in tsconfig.json to prevent accidental un-typed this references.
  • [x] Arrow functions use lexical this and cannot have a fake this parameter.