首页 文章

Typescript 2.0 @types不会自动引用

提问于
浏览
13

使用TS 2.0 Beta我无法使新的@types工作 . 在我的代码中的某个地方:

import * as angular from 'angular';

TS 2.0 @types:

npm install --save @types/angular
tsc

编译器找不到d.ts文件:错误:(1,26)TS2307:找不到模块'angular' .

使用打字工具和全局(环境之前)依赖关系的当前(旧)方法没有问题 .

我希望d.ts查找能够自动使用2.0,如下所述:

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

也许我错过了什么?

3 回答

  • 14

    我和另一个文件有同样的问题 - tsc找不到node_modules / @ types / es6-shim . 明确地向tsconfig.json添加类型有助于:

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

    我在Visual Studio Code中看到的是仍然需要三斜杠参考 . tsconfig.json中的类型编译器选项将解决编译错误,但是VS Code没有解决此问题,并且当您在编辑器中打开文件时它将显示错误 .

    以下是节点的三斜杠参考示例:

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

    三斜杠参考可以在一个单独的文件中,它将全面应用于项目中的其他文件,但它必须与tsconfig.json文件位于同一文件夹中 .

  • 1

    我遇到了同样的问题,它会通过cli成功构建, tsc app.ts ,但是gulp构建会失败 . 在我的情况下,我需要确保gulp模块使用最新的打字稿版本进行编译,而不是与模块捆绑在一起的版本,即 tsify ,传递参考更新的编译器: .plugin(tsify, {typescript: require('typescript')}) .

    不确定这是否适用于您的情况 .

相关问题