lib.d.ts doesn't have a definition for window.opera and is read-only, what do?

Topics: General
Nov 20, 2012 at 3:00 AM

window.opera is a common way for checking if the browser is using opera.  But when I use this in typescript it complains that "The property 'opera' does not exist on the value of type 'Window'.

 

How do I go about adding the opera object to window?

Coordinator
Nov 20, 2012 at 4:43 PM

You can extend an interface by declaring it multiple times.  For your example,  you could do so using:

interface Window {
  opera: any;
}

Unfortunately, there's a current limitation that you can't extend interfaces in lib.d.ts.  This is will be fixed in a future version.

Nov 21, 2012 at 12:10 AM

One of my work buddies figured it out for me.. Its a temporary solution.. but you can just do:

 

window['opera']

 

and that gets around the compile time error.

Coordinator
Nov 21, 2012 at 3:45 PM

Thanks for the tip!

Nov 27, 2012 at 8:04 PM

You can also do (<any>window).opera