Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'width' does not exist on type 'GlobalEventHandlers'

I would get the size of an image, following this tutorial, but I get a TypeScript error:

const img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

How to get image size (height & width) using JavaScript?

Property ‘width’ does not exist on type ‘GlobalEventHandlers’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What to do?

>Solution :

The load handler’s this isn’t typed to the image. Reference the image directly instead.

const img = new Image();
img.onload = function() {
  console.log(img.width + 'x' + img.height);
}

Or use addEventListener.

img.addEventListener('load', function() {
  console.log(this.width + 'x' + this.height);
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading