typescript 为什么我会收到错误:(45, 1) TS2304:找不到名称“angular”。在网络风暴中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29259527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting Error:(45, 1) TS2304: Cannot find name 'angular'. in WebStorm
提问by kpg
I am just starting out with TypeScript and trying to add it to an existing AngularJS project.
我刚刚开始使用 TypeScript 并尝试将它添加到现有的 AngularJS 项目中。
I have 'excluded' the bower directory where angular is installed and downloaded the definitelyTyped definitions for angular in the preferences window.
我已经“排除”了安装 angular 的 bower 目录,并在首选项窗口中下载了 angular 的绝对类型定义。
Angular code completion is working, but typescript is giving me an error TS2304 wherever I code 'angular.'.
Angular 代码完成工作正常,但打字稿在我编码“angular.”的任何地方都给我一个错误 TS2304。
What have I missed?
我错过了什么?
回答by lena
To fix the error, you need to copy the downloaded definitelyTyped TypeScript stubs from ~/Library/Caches/WebStorm9/extLibs
folder to your project directory and reference them in your .ts file using /// <reference path>
comments, like
要修复错误,您需要将下载的 absoluteTyped TypeScript 存根从~/Library/Caches/WebStorm9/extLibs
文件夹复制到您的项目目录,并使用/// <reference path>
注释在 .ts 文件中引用它们,例如
/// <reference path="path/to/angular.d.ts" />
Just to make things clear:
When you download Typescript stubs via Preferences/Languages & Frameworks/Javascript/libraries, they are placed into ~/Library/Caches/WebStorm9/extLibs
.
It's perfectly fine for Webstorm - it doesn't require placing library files directly in the project folder. Also, Webstorm itself doesn't need ///reference
comments to be able to resolve types - type hinting/navigation/completion works even if the types are not explicitly referenced. But the tsc compiler does need the d.ts files being placed somewhere in the project directory and referenced via ///reference
comment.
So, to get downloaded stubs available to typescript compiler, you need to copy/move them to you project directory (and probably rename to more human readable names :)) and add comments (can be done using 'Generate reference path comment
' intention (hit Alt+Enter on a reference to generate a comment)).
We plan to provide an option to download files directly to the project folder (instead of system/extLibs/ ) in the future versions
澄清一下:当您通过 Preferences/Languages & Frameworks/Javascript/libraries 下载 Typescript 存根时,它们会被放入~/Library/Caches/WebStorm9/extLibs
. Webstorm 完全没问题——它不需要将库文件直接放在项目文件夹中。此外,Webstorm 本身不需要///reference
注释来解析类型——即使类型没有被显式引用,类型提示/导航/完成也能工作。但是 tsc 编译器确实需要将 d.ts 文件放置在项目目录中的某个位置并通过///reference
注释引用。因此,要下载可供打字稿编译器使用的存根,您需要将它们复制/移动到您的项目目录(并可能重命名为更易读的名称:))并添加注释(可以使用'Generate reference path comment
' 意图(在引用上按 Alt+Enter 以生成评论))。我们计划在未来的版本中提供一个选项,将文件直接下载到项目文件夹(而不是 system/extLibs/ )
回答by James Manning
probably need to add a reference to the .d.ts in the particular .ts file having the problem, something like:
可能需要在有问题的特定 .ts 文件中添加对 .d.ts 的引用,例如:
/// <reference path="../typings/angular.d.ts"/>
回答by basarat
downloaded the definitelyTyped definitions for angular in the preferences window.
在首选项窗口中下载了 angular 的绝对类型定义。
The intent of downloading the stubs like that is to provide intellisense for JavaScriptcode not TypeScriptcode. For TypeScript code you should download and reference the .d.ts
directly in your TypeScript code.
下载这样的存根的目的是为JavaScript代码而不是TypeScript代码提供智能感知。对于 TypeScript 代码,您应该.d.ts
直接在 TypeScript 代码中下载并引用。