【问题标题】:TypeScript: Cannot find namespace 'ng'TypeScript:找不到命名空间“ng”
【发布时间】:2016-09-28 13:12:21
【问题描述】:

我正在尝试使用 tsconfig.json,这样我就可以避免在一堆文件的顶部。但我不断收到此错误:

[ts] 找不到命名空间“ng”。任何

我的tsconfig.json 文件如下所示:

{
    "compileOnSave": true,
    "compilerOptions": {
        "noImplicitAny": false,
        "target": "es2015",
        "allowSyntheticDefaultImports": true
    },
    "files": [
        "./typings/index.d.ts"
    ]
}

如您所见,我将其指向我的index.d.ts 文件,如下所示:

/// <reference path="globals/angular-ui-router/index.d.ts" />
/// <reference path="globals/angular/index.d.ts" />
/// <reference path="globals/jquery/index.d.ts" />

但是,如果我将此引用放在模块的顶部:

app.controller.js

/// <reference path="../../../../typings/index.d.ts" />

...

它识别ng 并给我它的类型。为什么这可能不起作用?

感谢任何帮助。提前致谢!

【问题讨论】:

  • 如问题中所述,我使用的是tsconfig.json 文件。
  • 您使用的是什么版本的打字稿? tsconfig.json 功能从 1.5 版开始添加。
  • 我在 gulp 任务中使用打字稿。使用gulp-typescript (2.13.5) 和tsify (0.16.0)。

标签: javascript angularjs typescript typescript-typings


【解决方案1】:

app.controller.js

您的文件具有.js 扩展名,因为它不是与tsconfig.json 匹配的文件的一部分。

快速解决方案

重命名为.ts 并在tsconfig.json 中列出文件

【讨论】:

  • 抱歉,打错了。我的错。该文件名为app.controller.ts。我不应该将我的类型文件路径添加到tsconfig.json 中的files 数组,而不是源文件(即app.controller.ts)吗?
  • 只需添加您的主文件和类型文件。
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 2021-11-18
  • 2021-01-21
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多