TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

TS 1.5.3 - Property 'attachEvent' does not exist on type 'Window'

Open Vadorequest opened this issue 11 years ago • 13 comments

I've just updated from 1.4.1 to 1.5.3 and my compiler doesn't want to compile that kind of code.

        if (window.addEventListener) {
            addEventListener("message", callback, false);
        } else {
            // Compatibilité IE.
            attachEvent("onmessage", callback);// THIS FAILS in 1.5.x., tried window.attachEvent, same thing.
        }

It is not listed on the breaking changes: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes

Any advice/workaround? Thanks.

(that seems to be the only breaking change, keep up the good work guys, TS is just awesome!)

Vadorequest avatar Jul 21 '15 15:07 Vadorequest

as for the workaround, what about

(<any>window).attachEvent("onmessage", callback);

nippur72 avatar Jul 21 '15 16:07 nippur72

This workaround should make it, thanks. ;)

Vadorequest avatar Jul 21 '15 16:07 Vadorequest

Sorry about that. i have added it to the breaking changes list. they are IE-specific and we have cleaned up most of these, to keep the library accurate. to get your code to work all you need is the defintiion for these, so include this in one of your files:

// Add IE-specific interfaces to Window
interface Window {
    attachEvent(event: string, listener: EventListener): boolean;
    detachEvent(event: string, listener: EventListener): void;
}

mhegazy avatar Jul 21 '15 16:07 mhegazy

It makes sense! Thanks for the quick review and help, much appreciated!

Vadorequest avatar Jul 21 '15 16:07 Vadorequest

Just another thing, I just saw you made a spelling mistake in the breaking change page at detatchEvent:

IE-specific attachEvent and detatchEvent are removed from Window

Vadorequest avatar Jul 21 '15 16:07 Vadorequest

thanks! fixed.

mhegazy avatar Jul 21 '15 16:07 mhegazy

so how come all the global properties such as window.HTML_Any_Element do not exist in the Window interface? am I doing something wrong?

z-vr avatar Jan 24 '16 01:01 z-vr

@z-vr, this is not related to the OP. the standard lib does not model the DOM mapping of global object to window. if you want to add these you can define something like:

interface Window {
    HTMLDivElement: typeof HTMLDivElement;
}

mhegazy avatar Jan 25 '16 17:01 mhegazy

Hi ,

I have similar issue with my web application. uses web parts and Ajax. Below is the error:-

Object doesn't support property or method 'attachEvent' Can someone help?

Thanks

mounikapk avatar Jan 25 '18 12:01 mounikapk

Try window['attachEvent']

kmvan avatar Jul 02 '18 15:07 kmvan

@kmvan Thanks. It works when I try window['FileReader']

mapinetree311 avatar Aug 15 '18 19:08 mapinetree311

It is better to have definitions for missing functions. I started a small library long time ago: https://github.com/NN---/lib.ie.d.ts/ Since I didn't need more IE-specific functions, it doesn't include more. You are free to use it and submit updates.

Currently it is not pushed to npm cause I don't know how much interest there is in such lib.

NN--- avatar Jan 20 '19 11:01 NN---

i've written some collections for typescript globals.d.ts#L105

dimaslanjaka avatar Nov 18 '21 11:11 dimaslanjaka