2015-09-04 110 views
0

我遵循pre-fetch example from the Typeahead site,但我正在使用Typescript。Twitter Typeahead - 打字稿中的Bloodhound错误

我拉了typeahead.d.ts file,没关系,直到我尝试使用Bloodhound实例作为数据集源。

的例子是这样的:

var countries = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.whitespace, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    // url points to a json file that contains an array of country names, see 
    // https://github.com/twitter/typeahead.js/blob/gh-pages/data/countries.json 
    prefetch: '../data/countries.json' 
}); 

// passing in `null` for the `options` arguments will result in the default 
// options being used 
$('#prefetch .typeahead').typeahead(null, { 
    name: 'countries', 
    source: countries 
}); 

所以我翻译成打字稿看起来是这样的:

// typeahead for the employees box 
var emps = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.whitespace, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    prefetch: { url: prefetchUrl } 
}); 

$("#employee").typeahead(null, { 
    name: "emps", 
    source: emps 
}); 

要么我已经翻译了JS错误或d.ts文件是错误的,因为发生以下错误:

类型'{name:string;来源:Bloodhound < {}>; }'不能分配给'Dataset'类型的参数。

属性“来源”的类型不兼容。 (查询:字符串,cb:(结果:任何)=>无效)=>无效'。'}不可分配给类型'(查询:字符串,cb:(结果:任何)=>无效)=>无效'。

任何人都可以告诉我我做错了什么或我需要在d.ts文件中修改以获得此编译?

由此创建的JavaScript实际上正常工作,但Typescript错误阻止了我的项目建立。

干杯。

回答

0

DataSet定义(你怎样分配来源是)是:

source: (query: string, cb: (result: any) => void) => void; 

如果你想传递一个Bloodhound对象的类型。

我会假定实际API已经发生了变化,或者d.ts文件只是不是最新的。

进一步探索看来该库确实需要对源Bloodhound对象项目的源代码后...

// use duck typing to see if source is a bloodhound instance by checking 
// for the __ttAdapter property; otherwise assume it is a function 
this.source = o.source.__ttAdapter ? o.source.__ttAdapter() : o.source; 

Reference

话虽这么说,我会更新绝对是d.ts通过拉动请求或与作者联系进行打字。

编辑

可能更新到d.ts:

source: (query: string, cb: (result: any) => void) => void | Bloodhound; 
+0

我很想更新d.ts文件,但我以前从未编写过一个。我尝试了一些调整,但只是打破了:(时间做一些阅读也许。 – Nick

+0

@尼克我只是更新答案,以尝试在你的本地d。如果这样的作品,你可以创建/提交一个拉请求,肯定键入 – Brocco

+0

关闭,但编辑抛出错误“通用类型'Bloodhound '需要1个类型参数。 – Nick