Visual Studio Code 抱怨它“找不到命名空间".对于 *.d.ts 文件中定义的类型 [英] Visual Studio Code complains that it "Cannot find namespace" for types defined in *.d.ts files
问题描述
我使用 gulp-angular Yeoman 生成器创建了一个新项目,语言设置为打字稿.然后运行 Gulp 构建过程,并在网络浏览器中打开页面,所有这些都可以正常工作,没有任何更大的问题.我只需要将 tsd.json
中的 ref: "master"
替换为 ref: "1.4.1"
即可使构建工作.基本上我执行了以下命令:
I created a new project using the gulp-angular Yeoman generator with language set to TypeScript. Then ran the Gulp build process and also opened the page in a web browser, which all worked without any bigger problems. I only had to replace ref: "master"
in the tsd.json
with ref: "1.4.1"
to make the build work. Basically I executed following commands:
yo gulp-angular
vim tsd.json
gulp
gulp serve
code .
之后我在 Visual Studio Code 中打开了项目.
Afterwards I opened the project in Visual Studio Code.
现在,Visual Studio Code 抱怨例如 找不到命名空间 'ng'" 每次出现 AngularJS 数据类型被使用.它还抱怨 MomentJS 和 *.d.ts
文件中定义的其他类型.
Now, Visual Studio Code complains for example that it "Cannot find namespace 'ng'" for each occurrence where AngularJS data types are used. It also complains about MomentJS and other typings defined in *.d.ts
files.
项目的 tsd.json
如下所示:
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "1.4.1",
"path": ".tmp/typings",
"bundle": ".tmp/typings/tsd.d.ts"
}
.tmp/typings
文件夹包含以下文件:
The .tmp/typings
folder contains following files:
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
文件:
To give an example of one of the source files where Visual Studio Code is complaining, here is the navbar.directive.ts
file:
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 Code 抱怨 ng.IDirective
类型它找不到命名空间 'ng'" 并且它抱怨 moment.MomentStatic
键入它找不到命名空间'moment'".
In this file Visual Studio Code is complaining about the ng.IDirective
type that it "Cannot find namespace 'ng'" and it is complaining about the moment.MomentStatic
type that it "Cannot find namespace 'moment'".
通过在 navbar.directive.ts
的顶部添加以下内容来显式引用类型定义文件可以解决问题:
Explicitely referencing the type definition files by adding the following to the top of navbar.directive.ts
removes the problem:
/// <reference path="../../../../.tmp/typings/angularjs/angular.d.ts"/>
/// <reference path="../../../../.tmp/typings/moment/moment.d.ts"/>
但是这些文件已经在.tmp/tsd.d.ts
中被引用,其中包含以下内容:
But these files are already referenced in .tmp/tsd.d.ts
, which contains the following:
/// <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" />
所以应该不需要显式引用文件?
So there should be no need to explicitly reference the files?
推荐答案
找不到命名空间ng""
Cannot find namespace 'ng'"
确保项目不包含对 declare module ng
的引用.暂时一样.
Make sure the project contains no reference to declare module ng
. Same for moment.
基于问题中的
通过在 navbar.directive.ts 顶部添加以下内容来显式引用类型定义文件可以解决问题
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
这篇关于Visual Studio Code 抱怨它“找不到命名空间".对于 *.d.ts 文件中定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!