How do I use XMLHttpRequest in Typescript?
The reason you can't specify this
is because you are using an arrow function =>
. You just need to change the type of the parameter:
request.onerror = (e: ProgressEvent) => {}
You don't really need to specify the type at all as it is inferred based on the type of onerror
request.onerror = (e) => { e // is ProgressEvent}
If you use a regular function you can specify this
request.onerror = function(this: XMLHttpRequest, e: ProgressEvent) { this // is XMLHttpRequest}
Although you don't really need to as it will be implicitly typed based on the type of onerror
request.onerror = function(e: ProgressEvent) { this // is still XMLHttpRequest}