I'm working on a small async library for my projects. I decided to code it in TypeScript, but my compiler is throwing me an error that 'XMLHttpRequest' does not exist on type 'Window'
, as the title says.
What I wanted to achieve is creation of ActiveXObject if window has no XMLHttprequest.
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
It's not really necessary for me to include it, but I'm wondering why is that?
IDE I'm using is VS Code (which also shows me the error) and I'm compiling with gulp-tsify
Try this:
if ((<any>window).XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
The thing is that Typescript
has a type for every object where it defines the properties of that type, sometimes there are missing properties (or properties that are added dynamically later) from those definitions, if you cast it to type any
then it will deal with it as an anonymous type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With