Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Vadorequest opened this issue Jul 21, 2015 · 13 comments
Closed

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

Vadorequest opened this issue Jul 21, 2015 · 13 comments
Labels
Breaking Change Would introduce errors in existing code

Comments

@Vadorequest
Copy link

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!)

@nippur72
Copy link

as for the workaround, what about

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

@Vadorequest
Copy link
Author

This workaround should make it, thanks. ;)

@mhegazy
Copy link
Contributor

mhegazy commented Jul 21, 2015

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 mhegazy added the Breaking Change Would introduce errors in existing code label Jul 21, 2015
@mhegazy mhegazy added this to the TypeScript 1.5 milestone Jul 21, 2015
@mhegazy mhegazy closed this as completed Jul 21, 2015
@Vadorequest
Copy link
Author

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

@Vadorequest
Copy link
Author

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

@mhegazy
Copy link
Contributor

mhegazy commented Jul 21, 2015

thanks! fixed.

@z-vr
Copy link

z-vr commented Jan 24, 2016

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?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 25, 2016

@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;
}

@NN--- NN--- mentioned this issue Feb 7, 2017
@mounikapk
Copy link

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

@kmvan
Copy link

kmvan commented Jul 2, 2018

Try window['attachEvent']

@wei-li-2016
Copy link

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

@NN---
Copy link

NN--- commented Jan 20, 2019

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.

@dimaslanjaka
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code
Projects
None yet
Development

No branches or pull requests

9 participants