2017-10-16 75 views
1

请让我知道我是否遵循正确的方法。如果不是,请建议我正确的做法。在Typescript-Angular中扩展没有打字稿定义的Javascript库

我在Typescript(Angular 4)中使用了一些JavaScript库。我有javascript函数的代码片段:

(function(Handsontable){ 
    var CustomEditor = Handsontable.editors.BaseEditor.prototype.extend(); 

    // ...rest of the editor code 

    // Put editor in dedicated namespace 
    Handsontable.editors.CustomEditor = CustomEditor; 

    // Register alias 
    Handsontable.editors.registerEditor('theBestEditor', CustomEditor); 

})(Handsontable); 

我已经延长在这样棱角分明打字稿文件这个库:

declare var Handsontable : any; 
constructor (
const autocompleteEditor2 = Handsontable.editors.AutocompleteEditor.prototype.extend(); 
Handsontable.editors.autocompleteEditor2 = autocompleteEditor2; 
Handsontable.editors.registerEditor('autocompleteEditor2', autocompleteEditor2); 
) 

回答

0
+0

我不想使用@ types/Handsontable,而是我将“声明var Handsontable:any;”我会扩展它的原型。这是一种IIFE - 立即在使用Typescript的Angular 4中调用函数表达式。请在这方面帮助我。 –

+0

如果您不想为此下载环境声明,那就不要这样做。手工制作的环境声明完全没有意义,而且'var foo:any'不会添加任何值,它只是忽略编译器警告。唯一的办法是下载该库的环境声明,你应该这样做,因为这是TS最强大的功能之一。 – Hitmands

+0

我们有的问题是我们正在使用* declare var foo:any *长时间。现在项目正在进行中。但是我们有具体的要求来定制* foo *。所以我们不能这么做。这就是我要求的原因是否有任何方法可以自定义* foo * * javascript原型* * typescript * –