David Michael Gang
David Michael Gang

Reputation: 7299

using documentMode with typescript

I check if the browser is ie by:

function isBrowserIE() {
  return window.document.documentMode;
}

Typescript raises an error:

Error TS2339: Property 'documentMode' does not exist on type 'Document'.

This is due to the change made in typescript compiler at version 1.5 :

Properties documentMode, parentWindow, createEventObject are removed from type Document

How can i get rid from the error?

Upvotes: 18

Views: 9415

Answers (3)

Diriector_Doc
Diriector_Doc

Reputation: 610

I like to use @ts-ignore since I know that document.documentMode will never cause a problem when it's compiled.

// @ts-ignore
var isIE = !!document.documentMode;

Upvotes: 0

user5480949
user5480949

Reputation: 1668

I've used bracket notation to get rid of the error:
document['documentMode']

Upvotes: 24

Nitzan Tomer
Nitzan Tomer

Reputation: 164139

You can simply add it to the Document interface:

interface Document {
    documentMode?: any;
}

function isBrowserIE() {
    return window.document.documentMode;
}

Edit

If you are using modules then you need to use global augmentation:

declare global {
    interface Document {
        documentMode?: any;
    }
}

Upvotes: 19

Related Questions