TS2683: this implicitly has type any
Fix TypeScript error TS2683 'this implicitly has type any'. Learn how to type this in callbacks, use arrow functions, and configure noImplicitThis.
What Does This Error Mean?
TypeScript error TS2683 occurs when 'this' is used in a context where its type cannot be inferred. This commonly happens in callbacks, event handlers, or class methods where 'this' loses its lexical scope. TypeScript requires 'this' to have an explicit type or be captured by an arrow function.
Common Causes
Using a regular function as a callback where 'this' becomes the global object or undefined
Passing a class method as a callback without binding 'this'
Event handlers defined as regular functions instead of arrow functions
Using 'this' inside a nested function within a method
noImplicitThis is enabled in tsconfig.json (recommended)
How to Fix It
Use arrow functions to preserve lexical scope
Arrow functions capture 'this' from the surrounding context.
class Counter {
count = 0
// ❌ Regular function loses this context
startBad() {
setInterval(function() {
this.count++ // TS2683: this implicitly has type any
}, 1000)
}
// ✅ Arrow function preserves this
startGood() {
setInterval(() => {
this.count++ // ✅ Correctly typed
}, 1000)
}
}Bind this explicitly
Use .bind() to bind the correct this context to a function.
class Logger {
prefix = "[App]"
log(message: string) {
console.log(`${this.prefix}: ${message}`)
}
setup() {
// ❌ Loses this when passed as callback
button.addEventListener("click", this.log)
// ✅ Bind this explicitly
button.addEventListener("click", this.log.bind(this))
// ✅ Or wrap in arrow function
button.addEventListener("click", (e) => this.log("clicked"))
}
}Type this explicitly in callbacks
Use the this parameter to declare the expected this type.
// Type the this parameter explicitly
function handleClick(this: HTMLButtonElement, event: MouseEvent) {
this.disabled = true // ✅ this is typed as HTMLButtonElement
}
button.addEventListener("click", handleClick)
// For object methods with explicit this
interface User {
name: string
greet(this: User): string
}
const user: User = {
name: "Alice",
greet(this: User) {
return `Hello, ${this.name}`
}
}Related Tools
Use these tools to debug and fix this error:
Related Errors
Other common errors in this category:
TS2304: Cannot find name
Fix TypeScript error TS2304 'Cannot find name'. Learn how to declare types, install type definitions, and configure tsconfig.json properly.
TS2339: Property does not exist on type
Fix TypeScript error TS2339 'Property does not exist on type'. Learn to type objects correctly, use type assertions, and narrow types with guards.
TS18048: 'x' is possibly 'undefined'
Fix TypeScript error TS18048 'is possibly undefined'. Learn to use optional chaining, nullish coalescing, and strict null checks correctly.
TS7016: Could not find a declaration file for module
Fix TypeScript error TS7016 'Could not find a declaration file for module'. Learn to install @types, create custom .d.ts files, and configure typeRoots.