23

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.

Community
  • 1
  • 1
Ciel
  • 17,312
  • 21
  • 104
  • 199

3 Answers3

43

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.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • The short answer is because my knowledge of javascript has not yet evolved to a point at which I am capable of quickly writing my own definition files. As I get more experienced, I'll probably be capable of doing that more. – Ciel Oct 06 '12 at 00:24
  • @Ciel If nobody has already written a definition file, sure, I can understand why you wouldn't want to do it yourself. But the jQuery one already exists, you can [copy it from here](http://typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#samples%2fjquery%2fjquery.d.ts). – Peter Olson Oct 06 '12 at 00:26
  • It's also worth noting that if something is missing from the jQuery definition file that ships with TypeScript you may need to add it yourself. You can either copy the shipped jquery.d.ts file and modify that or add your extensions via a seperate ext.d.ts file. See my answer on [adding jQuery plugins to TypeScript](http://stackoverflow.com/questions/12719529/using-jquery-plugin-in-typescript/12722003#12722003) for info on doing that. – Steven Ickman Oct 06 '12 at 00:45
  • Definition file also here: https://github.com/borisyankov/DefinitelyTyped (and on nuget if you happen to use visual studio) – nite Mar 05 '13 at 00:51
  • 1
    `declare var $:any;` better – Omar Aug 25 '18 at 18:03
8

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.

Murat Sutunc
  • 943
  • 5
  • 9
3

Typescript allows you to declare variables in the descired scope using the declare variable or declare function syntax (see Section 1.1 on page 9 in the language specification). However, using ambient declarations can only be a short-term solution since you will effectively loose all of Typescript's static type checking and hence one of the most important advantages of Typescript over Javascript.

Valentin
  • 7,874
  • 5
  • 33
  • 38