UberMouse
UberMouse

Reputation: 937

Use document.evaluate in TypeScript

How can I use document.evaluate in TypeScript? It's not a method of the Document type, so when I try and use it TypeScript generates an error and doesn't let me compile the code.

Is there some way I can add it to the definition of Document or ignore the compile error?

Upvotes: 0

Views: 528

Answers (1)

Ken Smith
Ken Smith

Reputation: 20445

There are several ways to do this. Since interfaces in TypeScript are extensible, you can just add the required method to the Document interface, like so:

interface Document{
    evaluate(xpathExpression:any, 
        contextNode?:any, 
        namespaceResolver?:any, 
        resultType?:any, 
        result?:any);
}

Or you could cast the document to any before calling the evaluate method:

(<any>document).evaluate('...');

Something along those lines at any rate.

Upvotes: 2

Related Questions