【问题标题】:Missing TypeScript type definitions for the @Angular packages in Angular 2Angular 2 中的 @Angular 包缺少 TypeScript 类型定义
【发布时间】:2016-08-31 18:12:44
【问题描述】:

现在我已将 my application 更新为 Angular2 v2.0.0-rc.1,当我的应用程序捆绑在 webpack 中时,我再次看到 TypeScript 编译错误/警告消息。我从我的 TypeScript 源文件中引用的任何 @angular 包都会出现这些消息,例如:

ERROR in ./src/app/app.ts
(1,34): error TS2307: Cannot find module '@angular/core'.

ERROR in ./src/app/app.ts
(3,76): error TS2307: Cannot find module '@angular/common'.

ERROR in ./src/app/app.ts
(4,30): error TS2307: Cannot find module '@angular/http'.

在 Angular2 的早期 beta 版本中,我通过在我的 app.ts 文件的顶部包含类似的内容,解决了 PromiseMap 等类的类似消息问题。

///<reference path="node_modules/angular2/typings/browser.d.ts"/>

我可以参考@angular Angular2 包的d.ts 文件来解决问题吗?到目前为止,typings 系统似乎没有任何可用的东西:

MFO-Mac:angular2-oauth2 mfo$ typings search '@angular'
No results found for search

现在我将我的 TypeScript tsconfig.json 文件配置为针对 ES6。但是,如果我将其改为针对 ES5,我不会收到这些 @angular 错误,而是会收到常见 ES6 类的错误,例如 PromiseSetMap。这是我为 ES6 配置的文件:

{
  "version": "1.6.2",
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true
  },
  "exclude": [
    "node_modules",
    "bower_components",
    "bootstrap"
  ],
  "files": [],
  "definitions": [
  ]
}

我怀疑node_modules/@angular 中的文件可以在tsconfig.json 文件的definitions 部分列出,但我目前对 TypeScript typedef 文件的工作方式了解不够。

如果有其他方法可以解决这个问题,我当然也愿意接受。

【问题讨论】:

标签: javascript angularjs typescript angular webpack


【解决方案1】:

如果你告诉它你正在使用 Node/NPM 样式的模块,TypeScript 编译器似乎足够聪明,可以自行判断文件定义文件。

通过将"moduleResolution": "node", 添加到我的tsconfig.json 文件中,问题消息消失并且应用程序继续按预期工作。

这是我的新文件(新添加的是第 4 行):

{
  "version": "1.6.2",
  "compilerOptions": {
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true
  },
  "exclude": [
    "node_modules",
    "bower_components",
    "bootstrap"
  ],
  "files": [],
  "definitions": []
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2021-11-02
    • 1970-01-01
    • 2019-05-12
    • 2017-07-12
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多