Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving from typings to @types using Visual Studio & Typescript 2.0.3

I've tried to remove typings from my web project (Visual Studio 2015 Community) and install d.ts files via new NPM @types (typescript 2.0.3) in package.json:

 "dependencies": {
    "@types/angular": "^1.5.8",
    "@types/angular-cookies": "^1.4.2",
    "@types/angular-local-storage": "^0.1.33",
    "@types/angular-material": "^1.1.37",
    "@types/angular-translate": "^2.4.33",
    "@types/lodash": "^4.14.36"
  }

Visual Studio's IntelliSense worked nicely with typings before because I included typings folder in my VS project. NPM installs types into node_modules/@types folder. Now here is my problem. I don't really want to include anything from node_modules in VS project. node_modules folder should be fine to get deleted and recreated again by npm at will. Visual Studio does not recognize the typings installed without them being included in the project! I guess I could create a file with ///reference tags in it but then I would have to maintain this file manually when installing/removing typings.

Is there any recommended way to make VS IntelliSense work?

like image 245
George Knap Avatar asked Sep 23 '16 12:09

George Knap


People also ask

Does VS code support TypeScript?

Visual Studio Code includes TypeScript language support but does not include the TypeScript compiler, tsc . You will need to install the TypeScript compiler either globally or in your workspace to transpile TypeScript source code to JavaScript ( tsc HelloWorld. ts ). You can test your install by checking the version.

What does @types mean NPM?

The @types npm organization is for obtaining type definitions with npm . Using these type definitions is a feature is coming in TypeScript 2.0. This will replace the current projects/tools such as typings and tsd, though these will continue to be supported for some time. Follow this answer to receive notifications.

Does Visual Studio support TypeScript?

TypeScript supportBy default, Visual Studio 2022 provides language support for JavaScript and TypeScript files to power IntelliSense without any specific project configuration. For compiling TypeScript, Visual Studio gives you the flexibility to choose which version of TypeScript to use on a per-project basis.


Video Answer


1 Answers

I was struggling with this same problem after making the switch to 2.0 and using the new @types convention.

I found this useful property after looking into the spec for the tsconfig.json here: http://json.schemastore.org/tsconfig

"typeRoots" property of compilerOptions.

I could not get the files or include arrays to pull in my typings, but this seems to have done the trick for me.

My tsconfig.json file as an example:

{
  "compileOnSave": true,
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "typeRoots": [
      "node_modules/@types"
    ]
  }
}

Hope this helps someone with the same issue.

like image 192
Miek Avatar answered Sep 19 '22 09:09

Miek