VS2015 中 jQueryUI AutoComplete 的 TypeScript 错误

TypeScript error with jQueryUI AutoComplete in VS2015

我是 运行 Visual Studio 2015 Professional Update 2,在为客户系统构建解决方案时经常遇到 TypeScript 错误。具体错误是:

TS2345 Argument of type
    '{
        source: (req: any, resp: any) => void;
        focus: (event: any, ui: any) => void;
        minLength: number;
        ...'
    is not assignable to parameter of type 'AutocompleteOptions'.
    Object literal may only specify known properties, and 'focus' does not exist
    in type 'AutocompleteOptions'.

查看了 jQueryUI documentation for autocomplete,这似乎是合法的。

导致此错误的代码片段(来自 init() 函数)如下:

$(lookupElem).autocomplete({
    source: (req, resp) => this.onAutoCompleteRequest(req, resp),
    focus: (event, ui) => {
        // Don't populate input field with selected value (pxid)
        event.preventDefault();
    },
    minLength: 3,  // Don't call autocomplete until specified number of characters have been input
    select: (e, ui) => this.onItemSelected(e, ui)
});

这在 VS2013 中有效,所以我不确定为什么 VS2015 会出现问题。这导致解决方案的其余部分(例如 Azure 组件、单独的 MVC 网站)构建失败,而且它正在从我的源文件夹中删除现有的 .js.js.map 文件。

我怎样才能:

我的 TypeScript 工具版本是 1.8.24.0,在工具 => 扩展和更新中似乎是最新的。

非常感谢。

This has worked in VS2013, so I'm not sure why VS2015 is having an issue

这是因为 freshness 在 TypeScript 中的一个新特性:https://basarat.gitbooks.io/typescript/content/docs/types/freshness.html

快速修复

只需移动到一个变量:

const config = {
    source: (req, resp) => this.onAutoCompleteRequest(req, resp),
    focus: (event, ui) => {
        // Don't populate input field with selected value (pxid)
        event.preventDefault();
    },
    minLength: 3,  // Don't call autocomplete until specified number of characters have been input
    select: (e, ui) => this.onItemSelected(e, ui)

};
$(lookupElem).autocomplete(config);

更好地修复

发送 PR 给 definitely typed