Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can TypeScript interact with jQuery without a definition file?

Tags:

I have been attempting to get to know this new 'TypeScript' stuff, and I am a bit curious on something.

Can it still work with existing javascript frameworks like jQuery without the need for a definition file with all of those interfaces? I have been attempting to test this out manually, but so far am a little unsure of how far the functionality extends.

update

by 'work' I am referring to simple functionality, not IDE features like auto-completion.

like image 392
Ciel Avatar asked Oct 06 '12 00:10

Ciel


People also ask

Do you need to install type definitions for 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.

Can you use TypeScript with jQuery?

TypeScript. DefinitelyTyped” and install. This will add a jQuery version of Typescript with an extension same as of Typescript files.


2 Answers

The simple answer is yes.

TypeScript is able to interact fully with any existing Javascript library. You only need the definition file if you want tooling in the IDE to make it easier to use.

Also, if you don't include the definition file, the TypeScript compiler might get mad at you for using a variable that hasn't been defined in your code (like $). To get around that you might have to do something like

declare var $; 

That said, I'm not sure why you wouldn't want to use the jQuery definition file. It surely makes it much more pleasant to write jQuery with.

like image 83
Peter Olson Avatar answered Oct 19 '22 04:10

Peter Olson


Yes you can. For example just write:

declare var $; 

and you can basically use the JQuery framework without having to define anything else. This is also very handy when you are converting your existing libraries / porting code.

like image 34
Murat Sutunc Avatar answered Oct 19 '22 03:10

Murat Sutunc