Home > Net >  How to defined interface for "this" keyword of function
How to defined interface for "this" keyword of function

Time:09-06

Let's say I have 1 class and 1 function like the bellow

class A {
  
  functionA = functionA.bind(this)
  functionB (name : string) {
    return name
  }
}
const functionA = function () {
  const name = this.functionB("functionA")
  
}

Typescript cannot detect interface for "this" using in functionA, it say "this" as any How can I specify interface for "this" in functionA

CodePudding user response:

Using this parameters, you can annotate the type of this inside the function:

const functionA = function (this: A) {
  • Related