Error Encyclopedia
TS2683

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

1

Using a regular function as a callback where 'this' becomes the global object or undefined

2

Passing a class method as a callback without binding 'this'

3

Event handlers defined as regular functions instead of arrow functions

4

Using 'this' inside a nested function within a method

5

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: