24

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.

3 Answers 3

45

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
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 for info on doing that.
Definition file also here: github.com/borisyankov/DefinitelyTyped (and on nuget if you happen to use visual studio)
declare var $:any; better
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.