user1847624
user1847624

Reputation: 21

How can I do Autocomplete in Typescript ? Have jquery.d.ts & jquery.autocomplete.d.ts files

Hi I a m trying to do ""Autocomplete""" in TypeScript & Jquery.

 this.$("#testAuto").autocomplete({
           source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]
           });

But the compiler can not find the autocomplete in any of the d.ts files.

 **error TS2094: The property 'autocomplete does not exist on value of type 'JQuery'.**

I am using Jquery v2.

Please help !!

Upvotes: 1

Views: 5925

Answers (1)

basarat
basarat

Reputation: 275947

You need to add a reference to the autocomplete.d.ts file e.g.:

/// <reference path="path/to/your/jquery.autocomplete.d.ts" />

Although I suspect that you are using the wrong definition file and should be using this one: https://github.com/borisyankov/DefinitelyTyped/blob/master/jqueryui/jqueryui.d.ts

It contains your required definition:

interface JQuery {
    // ...

  autocomplete(): JQuery;
  autocomplete(methodName: string): JQuery;
  autocomplete(methodName: 'close'): void;
  autocomplete(methodName: 'destroy'): void;
  autocomplete(methodName: 'disable'): void;
  autocomplete(methodName: 'enable'): void;
  autocomplete(methodName: 'search', value?: string): void;
  autocomplete(methodName: 'widget'): JQuery;
  autocomplete(options: JQueryUI.AutocompleteOptions): JQuery;
  autocomplete(optionLiteral: string, optionName: string): any;
  autocomplete(optionLiteral: string, options: JQueryUI.AutocompleteOptions): any;
  autocomplete(optionLiteral: string, optionName: string, optionValue: any): JQuery;

    // ... 
}

Update: Quick useful solution Since you are using the bootstrap version, remove the reference to jquery.autocomplete.d.ts and jqueuryUI. And just have the following block before your code:

interface JQuery{
   autocomplete(config:{source:string[];});
}

Upvotes: 2

Related Questions