Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript and jQuery type. The right way to import jquery type?

Is it possible to include jquery.d.ts and use jQueryStatic type? I need something like

...
protected el : jQueryStatic;
...

No matter how I try I can not import jQueryStatic interface from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/jquery

Thanks a lot.

Update

/// <reference path="../../../../defs/jquery.d.ts" />
 error TS2304: Cannot find name 'jQuery'.

/// <reference path="../../../../defs/jquery.d.ts" />
import {jQuery} from 'jquery';
error TS2305: Module '"jquery"' has no exported member 'jQuery'.

Update

Working solution was to add type JQuery not jQuery.

/// <reference path="../../../defs/jquery.d.ts" />
...
protected $el: JQuery;
like image 596
zim32 Avatar asked Feb 09 '16 04:02

zim32


People also ask

Can you use TypeScript with jQuery?

Most likely you need to download and include the TypeScript declaration file for jQuery— jquery. d. ts —in your project. Then the compiler will resolve the definitions for jquery automatically.

Does jQuery need to be imported?

Why jQuery is not working in HTML? Be sure to import the jQuery library before your custom script is loaded. You will need to go the jQuery CDN and suss out a recent version 1 minified CDN URL.


1 Answers

Based on your code :

el : jQueryStatic

Since you used el I think you meant element and by that the correct interface is actually JQuery:

el : JQuery

But the question you asked for

You can use typeof to convert a variable into a type. More : https://basarat.gitbooks.io/typescript/content/docs/project/declarationspaces.html

Update

The JQuery (not the case) is declared globally. The following works fine:

import * as $ from 'jquery';
var el:JQuery;
like image 123
basarat Avatar answered Oct 04 '22 21:10

basarat