TS 1.5.3 - Property 'attachEvent' does not exist on type 'Window'
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!)
as for the workaround, what about
(<any>window).attachEvent("onmessage", callback);
This workaround should make it, thanks. ;)
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;
}
It makes sense! Thanks for the quick review and help, much appreciated!
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
thanks! fixed.
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, 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;
}
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
Try window['attachEvent']
@kmvan
Thanks.
It works when I try window['FileReader']
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.
i've written some collections for typescript globals.d.ts#L105