Skip to content

'this' implicitly has type 'any'

Error message

                'this' implicitly has type 'any'
              

Quick summary

With noImplicitThis enabled, TypeScript errors when `this` is used in a function without a determined type. This commonly happens in standalone functions.

Why this happens

  • Using this in a non-class function without a this parameter

  • Passing a class method as a callback without binding

  • Using this in a function that will be called with a different context

  • Event handlers where this is set by the framework

Minimal example

✗ Broken code
function handleClick() {
  console.log(this.value); // this is implicitly any
}
✓ Fixed code
function handleClick(this: { value: number }) {
  console.log(this.value);
}

Add an explicit this parameter to type the context.

How to diagnose

  • Check if the function is a class method (this is typed) or standalone (this is any)

  • Look for this usage in callbacks or event handlers

  • Check if noImplicitThis is enabled in tsconfig

How to fix

  • Add a this parameter: function foo(this: SomeType)

  • Use an arrow function (arrow functions don't have their own this)

  • Bind the function: this.method.bind(this)

  • Move the function into a class where this is determined

How to prevent

  • Use arrow functions for callbacks to preserve this

  • Enable noImplicitThis (part of strict mode)

  • Explicitly type this in standalone functions

Related Resources

Related Glossary

← Back to language