1

I just downloaded the typeahead.js d.ts file, but how the hell do you use it ?

The options object has to be written in this format:

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

But how do I define the source ?

This is causing a compiler error-:

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

Can someone help me with the syntax ?

EDIT: Solved it:

here is the code I used in typescript-: You need jquery, for $.each() function.

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);
    }
}
ng.newbie
  • 2,807
  • 3
  • 23
  • 57

1 Answers1

0

Reference your file, for example:

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

or

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

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

1) put the .d.ts file in your tsconfig.json as described here: Do I have to reference typescript definition in every file

2) try to call the method via namespace:

Twitter.Typeahead.callsomething()

or import it like

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

you also need to download jquery as this library depends on it (look at the .d.ts file).

Community
  • 1
  • 1
juFo
  • 17,849
  • 10
  • 105
  • 142
  • 1
    Already did that I am asking on how to write the code. What do I put in `$("#input").typeahead()` ? How do I declare the async and sync sources and also the query strings ? – ng.newbie Nov 29 '16 at 03:44