TypeScript · Middle · Question 36 of 75

How do you specify the type of this for a handler function?

function handler(this: HTMLButtonElement, e: MouseEvent) {
  this.disabled = true;
}

Explanation

The this pseudo-parameter describes which context the function must be called with; it isn't an argument. The compiler checks calls and uses of this inside. In arrow functions, this is lexical and can't be specified this way.

Further reading

Problem with a question

TypeScript · Middle · question 36

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz