1

Does anyone know why the hell i can't use jQuery in my TypeScript code (tho it looks like noone else doesn't have that problem).

I've made a new TypeScript project in VS2012, included the jquery.d.ts, added a script tag pointing to jquery cdn in the main html file, and in the main app.ts file i've added "

///  <reference path="jquery.d.ts" />

at the top of the file and underneath a simple line "$("something").hide();". The intellisense worked fine, tho when I try to compile it I get an error with code 1. I checked it in the command window and it said "The name '$' does not exist in the current scope".

Does anyone know whats going on? Generally I have a very hard time splitting my code to different files, I had to write the whole app in one file. I tried to use modules, import and export but everything ends up in a scope error thrown by the compiler :/

Please save me

EDIT @Sohnee Its a default Typescript project from VS:

Structure:

app.css
app.ts
  --app.js
default.htm
jquery.d.ts

Modified files:

1) app.ts:

/// <reference path="jquery.d.ts" />

$("body").hide();

2) default.htm

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
    <script src="http://code.jquery.com/jquery-1.8.0.js"></script>
    <script src="app.js"></script>
</head>
<body>
    <h1>TypeScript HTML App</h1>

    <div id="content"/>
</body>
</html>

2 Answers 2

1

Your reference looks incorrect.

/// <_reference path="jquery.d.ts" />"

Should be

/// <reference path="./jquery.d.ts" />

The only way I can recreate this issue is by making the reference path invalid.

For example, I get the same error as you in these situations:

Missing slash:

/// <reference path="jquery.d.ts">

Missing quote (or missing both quotes):

/// <reference path="jquery.d.ts />

Invalid path:

No code example for this one, but if you have jquery.d.ts in a different folder to your script, the path needs to be relative.

Works fine:

/// <reference path="./jquery.d.ts" />
Sign up to request clarification or add additional context in comments.

6 Comments

i couldnt write it that way - XSS protection on stackoverflow? how did you do it?
nvm got it :P anyway thats not the case
If you precede the code block with 4 spaces it normally allows anything in the code block. What was the issue in the end?
Damn we got on a wrong track :D I meant "i got it" with the code block issue. The error thing is still not resolved.
I have tested a ton of scenarios and listed the ones that can cause a problem in the answer. Also, check the contents of jquery.d.ts just to ensure that right at the bottom it has declare var $: JQueryStatic.
|
0

How does the jquery.d.ts looks like?

it should be something like

interface JQuery {
hide();
}
interface JQueryStatic {
(query: string): JQuery; 
(ready: () => any): any;
}
declare var $: JQueryStatic;

now add it as a dependency inside the app.ts like (not a big fan of putting periods in the filenames so im going to call it custom.ts

/// <reference path="custom.ts" />
$(function(){$("body").hide();});

now build the solution if you have the web essentials installed it wil generate the custom.js and app.js for you

now run the project, hopefully you'll not see a thing because the body will be hidden.

for more information Read the TypeScript Lang Specs

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.