TypeScript 错误:Webpack / Angular 2 / Typings

TypeScript Errors: Webpack / Angular 2 / Typings

我想知道如何消除 Typings/TypeScript 错误,例如:

Property 'modal' does not exist on type 'JQuery'.

据我所知,我已经为 JQuery 正确安装了最新的类型,而且它肯定能找到并使用它们(我已经对此进行了测试)。 'modal' 属性 实际上是我调用的一个函数:

$('#popup').modal();
// modal() is part of the Semantic UI library which is available and works in my browser

请注意,我使用的是最新版本的 Typings v1.0.4,它似乎更喜欢使用 globalDependencies 而不是 "ambient" 类型。我的 typings.json 文件:

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160317120654",
    "jasmine": "registry:dt/jasmine#2.2.0+20160505161446",
    "jquery": "registry:dt/jquery#1.10.0+20160417213236",
    "node": "registry:dt/node#4.0.0+20160509154515",
    "source-map": "registry:dt/source-map#0.0.0+20160317120654",
    "uglify-js": "registry:dt/uglify-js#2.6.1+20160316155526",
    "webpack": "registry:dt/webpack#1.12.9+20160523035535"
  }
}

有什么方法可以告诉打字,像 modal() 和许多其他函数实际上是可以的并且不会抛出错误吗?

更新: 根据下面的答案,我通过将以下内容添加到我加载的自定义类型文件中解决了这些错误 custom-typings.d.ts

interface JQuery {
  modal: (cmd?: string, options?: any) => JQuery;
  dropdown: () => void;
  checkbox: () => void;
  calendar: (options?:any) => void;
}

这告诉 Typescript JQuery 对象有额外的可用函数:modal()、dropdown() 等。

您会注意到,对于我的模态定义,我指定了两个可以传入的可选变量(因为这是我正在使用的语义 UI modal() 函数的行为)。 modal() 函数 returns 另一个 JQuery 递归对象,因为真实的对象也是这种方式——这允许我在一个链中多次调用它:

$('#someId').modal('setting', {autoFocus: true}).modal('show')

没有错。您收到此错误是因为 modal() 没有出现在 jquery.d.ts 中。要消除此错误消息,请参阅