简体   繁体   English

尽管存在angular.d.ts文件,TypeScript的VS2015内部版本仍给出“ TS2304:内部找不到角度”错误

[英]VS2015 build of TypeScript gives “TS2304:Build cannot find angular” error despite presence of angular.d.ts file

I'm new to TypeScript and have hit problems building my first VS2015 solution which uses the AngularJS framework and the Toastr library. 我是TypeScript的新手,在构建使用AngularJS框架和Toastr库的第一个VS2015解决方案时遇到了一些问题。

VS2015 is the RTM version running under Windows 10 Pro Edition. VS2015是在Windows 10专业版下运行的RTM版本。 TypeScript is version 1.6.2. TypeScript是版本1.6.2。 Angular is version 1.5.0-beta.2 Angular是1.5.0-beta.2版本

Attempts to reference angular or toastr in my TypeScript files give "TS2304: Build cannot find name 'angular'" (or 'toastr') errors when attempting to build the new project, despite having installed the appropriate d.ts files from Definitely Typed. 尝试在我的TypeScript文件中引用angular或toastr时,尽管已从Definitely Typed中安装了适当的d.ts文件,但尝试构建新项目时,生成“ TS2304:生成找不到名称“ angular””(或“ toastr”)错误。

The angular library definition file angular.d.ts is there under the typings directory in a sub-directory named angularjs and includes the line: 角度库定义文件angular.d.ts位于名为angularjs的子目录中的types目录下,并包括以下行:

declare var angular: angular.IAngularStatic; 

in the tsd.json file I see the following: 在tsd.json文件中,我看到以下内容:

  "installed": {
    "angularjs/angular.d.ts": {
      "commit": "e48f0c7a0a5f9e9d35e9cfd7506461bab435cb49"
    },

I was originally getting the error with Resharper 10 but have suspended it to remove that from the possible causes, but I get the TS2304 error when I try and build even with Resharper disabled. 我最初在使用Resharper 10时遇到错误,但已将其暂停以将其从可能的原因中消除,但是即使在禁用Resharper的情况下尝试构建时,也会出现TS2304错误。

What have I missed? 我错过了什么? How can I get TypeScript to recognise the two installed type definition files so I can successfully build my project? 如何获得TypeScript识别两个已安装的类型定义文件,以便成功构建项目?

I fixed this by adding the following line at the top of my TypeScript source file: 我通过在TypeScript源文件的顶部添加以下行来解决此问题:

/// <reference path="../../../../../typings/angularjs/angular.d.ts"/> 

The Pluralsight "TypeScript and AngularJS" course I used to learn TypeScript had not mentioned this "comment" command at all (I found it by googling the same issue raised as a problem with WebStorm). 我用来学习TypeScript的Pluralsight“ TypeScript和AngularJS”课程根本没有提到此“注释”命令(我通过搜索与WebStorm问题相同的问题来发现它)。 I had assumed that the reference in tsd.json should be enough for the TypeScript compiler to find the appropriate type specification descriptions, but apparently not! 我以为tsd.json中的引用应该足以让TypeScript编译器找到适当的类型规范描述,但显然不行! The course does assume VS Code rather than VS2015 for the IDE so maybe that is the issue? 该课程确实为IDE假定使用VS Code而不是VS2015,所以这可能是问题所在吗?

Are you sure that you have include angular.js in your html? 您确定您的html中包含了angular.js吗? declare var angular means that angular variable is created outside from any ts file and d.ts is used to infrom typescript what angular variable has. 声明var angular意味着从任何ts文件外部创建了角度变量,并且d.ts用于从打字稿中输入角度变量具有的内容。

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

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