13
votes

using TS 2.0 Beta I can't get the new @types working. somewhere in my code:

import * as angular from 'angular';

TS 2.0 @types:

npm install --save @types/angular
tsc

the compiler doesn't find the d.ts files though: Error:(1, 26) TS2307: Cannot find module 'angular'.

no issues with current (old) method of using the typings tool and global (before ambient) dependencies.

I expected the d.ts lookup to work automatically with 2.0 as described here:

https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/

perhaps I am missing something?

3
Running into the same issue, wondering if this didn't make it into the 2.0 beta release? Didn't see it mentioned in the wiki or their blog article. :/ - Mark Pieszak - Trilon.io
What editor are you using by the way? In vscode for example this is still a bug for example. Apparently tsc should work fine, it's just your editor that's complaining. - Mark Pieszak - Trilon.io
I am using the command line and local tsc direclty to rule out any issues related to IDEA - dmudro
Have you checked that you have actually updated the globally installed typescript with tsc --version? - ligaz
Regarding the editor you can configure VSCode to use your locally installed version of the compiler: stackoverflow.com/a/32160966/6409 - ligaz

3 Answers

15
votes

I was having the same issue with another file - tsc didn't find node_modules/@types/es6-shim. Explicitly adding types to tsconfig.json helped:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "noEmit": true,
    "types":["es6-shim"],
    "sourceMap": true,
    "target": "es5"
  }
}
1
votes

What I am seeing in Visual Studio Code is that a triple-slash reference is still needed. The types compiler option in tsconfig.json will resolve compilation errors, but VS Code does not pick up on this, and it will show errors when you open the file in the editor.

Here is an example of a triple-slash reference for node:

/// <reference path="../node_modules/@types/node/index.d.ts" />

The triple-slash reference can be in a separate file, and it will apply across the board to other files in the project, but it has to be in the same folder as the tsconfig.json file.

0
votes

I was having the same problem, where it would build successfully via cli, tsc app.ts, but gulp build would fail. In my case, I needed to make sure the gulp modules were using the latest typescript version for compilation, not the version bundled with the module, i.e. for tsify, passing in reference to newer compiler: .plugin(tsify, {typescript: require('typescript')}).

Not sure if this applies to your situation.