Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Microsoft JScript runtime error: '$' is undefined" when using Typescript and jQuery

I'm getting this error trying a simple jQuery sample with Typescript:

"tsc.js(23915, 17) Microsoft JScript runtime error: '$' is undefined"

My entire .ts file:

/// <reference path="scripts/jquery-1.8.d.ts" />

$(document).ready(function () {});

The jquery-1.8.d.ts is from DefinitelyTyped, but I get the same error if I use the standard jquery.d.ts. I'm not getting any errors on the reference tag, it's correct and is finding the .d.ts file.

I'm obviously missing something basic here, I can't figure out why I'm getting this error. I've got the VS 2012 extension installed and am getting full autocomplete on jQuery... so when I type '$' I do get autcomplete popups. The generated .js file is correct, nothing wrong there. A much more complex .ts file is compiling correctly and the output .js file is perfectly fine, so this is more of an annoyance than an error, I suppose. Or am I missing other errors because of this runtime error??

I'm compiling by adding an "External Tool" in VS with command: C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.8.1.1\tsc.exe arguments: -e "$(ItemPath)" --sourcemap

like image 574
Eric Sassaman Avatar asked Dec 12 '12 18:12

Eric Sassaman


1 Answers

I believe the issue is with the command that you are using itself, or at least one of the options you are passing in. "-e" tells the compiler to "Execute the script after compilation". So what you see, "Microsoft JScript runtime error: '$' is undefined", is not a compile error, but rather a runtime error that is showing up after the file is compiled and is in the process of being executed.

Hope that helps!

like image 72
Alex Klock Avatar answered Sep 22 '22 23:09

Alex Klock