简体   繁体   中英

Typescript 2.0 @types not automatically referenced

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?

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"
  }
}

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.

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, ie for tsify , passing in reference to newer compiler: .plugin(tsify, {typescript: require('typescript')}) .

Not sure if this applies to your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM