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;
}- ✓Correct answer: A leading
this"parameter" sets the context type and is erased on compile - 2It's a regular first parameter that must be passed when calling
- 3This syntax is forbidden in TypeScript strict mode
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.