简体   繁体   English

Typescript 2.0 @types不会自动引用

[英]Typescript 2.0 @types not automatically referenced

using TS 2.0 Beta I can't get the new @types working. 使用TS 2.0 Beta我无法使新的@types工作。 somewhere in my code: 在我的代码中的某个地方:

import * as angular from 'angular';

TS 2.0 @types: 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'. 编译器找不到d.ts文件:错误:(1,26)TS2307:找不到模块'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: 我希望d.ts查找能够自动使用2.0,如下所述:

https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/ 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. 我和另一个文件有同样的问题 - tsc找不到node_modules / @ types / es6-shim。 Explicitly adding types to tsconfig.json helped: 明确地向tsconfig.json添加类型有助于:

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

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

I was having the same problem, where it would build successfully via cli, tsc app.ts , but gulp build would fail. 我遇到了同样的问题,它可以通过cli, tsc app.ts成功构建,但是tsc app.ts构建会失败。 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')}) . 在我的情况下,我需要确保gulp模块使用最新的打字稿版本进行编译,而不是与模块捆绑在一起的版本,即tsify ,传递参考更新的编译器: .plugin(tsify, {typescript: require('typescript')})

Not sure if this applies to your situation. 不确定这是否适用于您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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