Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find name '$' in TS file unless referenced in every file

I'm setting up typescript in Visual Studio 2015. I'm going to be using jquery with the TS files. When I use jquery, VS will underline the '$' and say cannot find name, and will not build successfully. The only way it will build is if I add the reference to jquery typings in each TS file. /// <reference path="typings/index.d.ts" /> Is there anyway to use this reference globally instead of adding it to every file?

In Visual Studio Code I dont have this issue.

My directory looks like this -Scripts --ts ---typings ---main.ts tsconfig.json --js

My tasks.json file in the root

    {
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "tsc",

    // The command is a shell script
    "isShellCommand": true,

    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",

    // Tell the tsc compiler to use the tsconfig.json from the open folder.
    "args": ["-p", "../Scripts/Weblink/ts"],

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
}

taskconfig.json in Scripts/ts

{
"compileOnSave": true,
"compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "outDir": "../lib/"
},
"exclude": [
    "typings/*"
]
}
like image 854
stanggt3 Avatar asked Dec 15 '16 16:12

stanggt3


1 Answers

In TypeScript 2.x you should install the typings like this:

npm install --save @types/jquery

And then:

import * as $ from "jquery";

No need to reference it, TypeScript will handle it automatically.

More info

like image 74
lenny Avatar answered Oct 25 '22 07:10

lenny