2015-09-04 29 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