'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
thisin a non-class function without a this parameterPassing a class method as a callback without binding
Using
thisin a function that will be called with a different contextEvent handlers where
thisis set by the framework
Minimal example
function handleClick() {
console.log(this.value); // this is implicitly any
}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
thisusage in callbacks or event handlersCheck if noImplicitThis is enabled in tsconfig
How to fix
Add a
thisparameter: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
thisis determined
How to prevent
Use arrow functions for callbacks to preserve
thisEnable noImplicitThis (part of strict mode)
Explicitly type
thisin standalone functions