2015-05-24 28 views
0

將函數添加到實際已存在的typescript的最佳方式是什麼?在我的情況下,這是Node.getAttribute(),它實際上內置於瀏覽器,但尚未被TypeScript識別。它的錯誤:Cannot find property 'getAttribute' on type 'Node'.在TypeScript中添加現有函數:Node.getAttribute()

在我來說,我就遇到了這個錯誤執行以下:

var xmlDocument = new DOMParser().parseFromString(xmlString, "text/xml"), 
    rootfile = xmlDocument.getElementsByTagName("aNode")[0]; 

console.log(rootfile.getAttribute("anAttribute")); 

這成功的瀏覽器,但通過打字稿拋出一個錯誤。

回答

1

由於接口是開放性只是告訴打字稿一下:

interface Node { 
    getAttribute(attr: string): string; 
} 

我建議在globals.d.ts文件在你的項目的根這樣做。

+0

我會努力。但是,當我修改* lib.d.ts *與相同的指令沒有任何反應...... – Roger

+0

您正在修改的'lib.d.ts'不一定是您的開發工具所使用的。 – basarat

相關問題