我使用
gulp-angular Yeoman生成器创建了一个新项目,语言设置为TypeScript.然后运行
Gulp构建过程并在Web浏览器中打开页面,所有这些都没有任何更大的问题.我只需要在tsd.json中用ref:“1.4.1”替换ref:“master”以使构建工作.基本上我执行了以下命令:
yo gulp-angular vim tsd.json gulp gulp serve code .
之后我在Visual Studio Code开了这个项目.
现在,Visual Studio代码抱怨例如,对于使用AngularJS数据类型的每次出现,它都“找不到命名空间’ng’”.它还抱怨MomentJS以及* .d.ts文件中定义的其他类型.
项目的tsd.json看起来像这样:
{ "version": "v4","repo": "borisyankov/DefinitelyTyped","ref": "1.4.1","path": ".tmp/typings","bundle": ".tmp/typings/tsd.d.ts" }
angular-ui-router/angular-ui-router.d.ts angularjs/angular-animate.d.ts angularjs/angular-cookies.d.ts angularjs/angular-mocks.d.ts angularjs/angular-resource.d.ts angularjs/angular-sanitize.d.ts angularjs/angular.d.ts jquery/jquery.d.ts moment/moment-node.d.ts moment/moment.d.ts toastr/toastr.d.ts tsd.d.ts
举一个Visual Studio Code抱怨的源文件的例子,这里是navbar.directive.ts文件:
module editorTs { 'use strict'; /** @ngInject */ export function acmeNavbar(): ng.IDirective { return { restrict: 'E',scope: { creationDate: '=' },templateUrl: 'app/components/navbar/navbar.html',controller: NavbarController,controllerAs: 'vm',bindToController: true }; } /** @ngInject */ class NavbarController { public relativeDate: string; constructor(moment: moment.MomentStatic) { this.relativeDate = moment(1444135396045).fromNow(); } } }
在这个文件中,Visual Studio代码抱怨ng.IDirective类型它“无法找到命名空间’ng’”并且它抱怨当时.MomentStatic类型它“无法找到命名空间’时刻’”.
编辑:
通过在navbar.directive.ts的顶部添加以下内容来显式引用类型定义文件,可以解决以下问题:
/// <reference path="../../../../.tmp/typings/angularjs/angular.d.ts"/> /// <reference path="../../../../.tmp/typings/moment/moment.d.ts"/>
但是这些文件已在.tmp / tsd.d.ts中引用,其中包含以下内容:
/// <reference path="angular-ui-router/angular-ui-router.d.ts" /> /// <reference path="angularjs/angular-animate.d.ts" /> /// <reference path="angularjs/angular-cookies.d.ts" /> /// <reference path="angularjs/angular-mocks.d.ts" /> /// <reference path="angularjs/angular-resource.d.ts" /> /// <reference path="angularjs/angular-sanitize.d.ts" /> /// <reference path="angularjs/angular.d.ts" /> /// <reference path="jquery/jquery.d.ts" /> /// <reference path="moment/moment-node.d.ts" /> /// <reference path="moment/moment.d.ts" /> /// <reference path="toastr/toastr.d.ts" />
那么应该没有必要明确引用文件?
解决方法
Cannot find namespace ‘ng'”
确保项目不包含声明模块ng的引用.同样的.
更新
基于问题中的编辑:
Explicitely referencing the type definition files by adding the following to the top of navbar.directive.ts removes the problem
请使用tsconfig.json:https://basarat.gitbooks.io/typescript/content/docs/project/compilation-context.html