打字稿:为现有 class 声明函数

Typescript: declare function for existing class

我正在尝试将此插件 this plugin 与打字稿一起使用(连同 Angular 2 和 Angular CLI)。

我必须像这样初始化一些元素:

Array.from(document.getElementsByClassName('i-checks')).forEach(function(item) {
        item.iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });

不幸的是typescript抛出错误,因为他找不到iCheck().

Module build failed: Error: /../my/path.ts (39,13): Unterminated string literal.
/../my/path.ts (35,18): Property 'iCheck' does not exist on type 'Element'.

如何让打字稿忽略这个错误?

我在index.html中包含了插件所需的CSS和JS文件。所以我认为它应该有效。

你应该:

1) 安装插件:

npm i -S icheck

2) 将引用复制到 angular-cli.json:

"scripts": ["../node_modules/icheck/icheck.js"]

3) typings.d.ts:

  declare ... function or what you need from icheck