Typescript: [lib.d.ts BUG] new window.Image(); //ERROR: Property 'Image' does not exists on type 'Window'

Created on 10 Aug 2016  ·  4Comments  ·  Source: microsoft/TypeScript

TypeScript Version: atom-typescript

Code

new Image(); //works
new window.Image(); //ERROR: Property 'Image' des not exists on type 'Window'

Expected behavior:
Image constructor should exists in window object

Actual behavior:
Image constructor does not exists in window object

Other Info:
I actually ussing nw.js so I can't access to the window object directly:

function newImage( win: Window): Image {
    return new win.Image(); //ERROR: Property 'Image' des not exists on type 'Window'
}

If I cast win as any, it works, but I loose the Typescript typing feature.

Question

All 4 comments

The library does not redefine all globals on window. if you want to use one of these on window you can add it, in one of your files add:

interface Window {
    Image: typeof Image;
}

Thanks @mhegazy. But where do you add this snippet?
image

this works for me:

    const image = new (window as any).Image()
    image.src = './dragFile.png'
    image.onload = () => {
      this.setState({
        image: image
      })
    }

source: https://stackoverflow.com/questions/50717127/new-window-image-is-not-working-with-typescript-react

interface Window {
    Image: typeof Image,
}

declare const window: Window;

Declare it after defining it and it should work fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  ·  3Comments

Roam-Cooper picture Roam-Cooper  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments

wmaurer picture wmaurer  ·  3Comments

fwanicka picture fwanicka  ·  3Comments