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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top