2016-11-28 15 views
1

我剛剛下載了typeahead.js d.ts文件,但是如何使用它?使用typeahead.js絕對鍵入的文件

選項對象具有這種格式寫入:

let a:Twitter.Typeahead.Options = {}; 

但是我怎麼定義源?

這導致一個編譯器錯誤 - :

let b:Twitter.Typeahead.Dataset<string> = 
{source:{query:'',syncResults:null,asyncResults:null}}; 

有人可以幫助我的語法?

編輯: 解決它:

這裏是我在typescript-使用的代碼:您需要的jQuery,對。每個$()函數。

private sourceFactory(list: string[]): 
     (query: string, syncResults: (results: string[]) => void) => void { 
    return (query: string, syncResults: (results: string[]) => void) => { 

     let matches: string[] = []; 

     let regex: RegExp = new RegExp('^' + query, 'i'); 

     $.each(list, (i: number, str: string) => { 
      if (regex.test(str)) { 
       matches.push(str); 
      } 
     }); 

     syncResults(matches); 
    } 
} 

回答

0

參考你的文件,例如:

/// <reference path="Validation.ts" /> 

/// <reference path="./../Scripts/typings/foo.d.ts" /> 

見:https://www.typescriptlang.org/docs/handbook/namespaces.html

1)把.d.ts文件在您tsconfig.json如下所述:Do I have to reference typescript definition in every file

2)嘗試通過命名空間中調用方法:

Twitter.Typeahead.callsomething() 

或導入它像

import tah from 'foo' 
import { default as tah } from 'foo' 

你還需要下載jQuery的,因爲這個庫依賴於它(看.d.ts文件)。

+0

已經做到了,我正在問如何編寫代碼。我在$(「#input」)中輸入了什麼?typeahead()'?我如何聲明異步和同步源以及查詢字符串? –