Visual Studio Code 抱怨它“找不到 *.d.ts 文件中定义的类型的命名空间”

zab*_*rob 6 angularjs yeoman typescript gulp visual-studio-code

我使用gulp-Angular Yeoman 生成器创建了一个新项目,并将语言设置为 TypeScript。然后运行​​Gulp构建过程并在 Web 浏览器中打开页面,所有这些都正常运行,没有出现任何更大的问题。我只需要替换ref: "master"为即可tsd.json使ref: "1.4.1"构建工作。基本上我执行了以下命令:

yo gulp-angular
vim tsd.json
gulp
gulp serve
code .
Run Code Online (Sandbox Code Playgroud)

然后我在Visual Studio Code中打开该项目。

现在,Visual Studio Code 会抱怨每次使用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"
}
Run Code Online (Sandbox Code Playgroud)

.tmp/typings文件夹包含以下文件:

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
Run Code Online (Sandbox Code Playgroud)

举一个 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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在此文件中,Visual Studio Code 抱怨类型ng.IDirective无法找到名称空间 'ng'”,并抱怨moment.MomentStatic类型“无法找到名称空间 'moment'”

编辑:

通过将以下内容添加到顶部来显式引用类型定义文件navbar.directive.ts可以消除该问题:

/// <reference path="../../../../.tmp/typings/angularjs/angular.d.ts"/>
/// <reference path="../../../../.tmp/typings/moment/moment.d.ts"/>
Run Code Online (Sandbox Code Playgroud)

但这些文件已在 中引用.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" />
Run Code Online (Sandbox Code Playgroud)

那么应该不需要显式引用这些文件?

bas*_*rat 1

找不到命名空间“ng””

确保该项目不包含对declare module ng. 暂时也一样。

更新

基于问题中的编辑:

通过将以下内容添加到 navbar.directive.ts 顶部来显式引用类型定义文件可以消除问题

请使用tsconfig.jsonhttps://basarat.gitbooks.io/typescript/content/docs/project/compilation-context.html

  • 唔。我遇到了同样的问题,我不明白“使用 tsconfig.json”的解决方案是什么。tsconfig.json 中有什么内容? (5认同)