【问题标题】:Angular2 beta 8 include Typescript definition files from node_modulesAngular2 beta 8 包含来自 node_modules 的 Typescript 定义文件
【发布时间】:2016-03-06 18:52:30
【问题描述】:

我有一个 Angular 2 beta 8 项目,可以使用 gulp。

问题在于 gulp-typescript 无法检查类型。 gulp 任务如下:

gulp.task('ts', function() {
  gulp.src('app/**/*.ts')
  .pipe(ts({
    target: 'ES6',
    emitDecoratorMetadata: true,
    experimentalDecorators: true
  }))
  .pipe(rename({ extname: '' }))
  .pipe(traceur({
      modules: 'instantiate',
      moduleName: true,
      annotations: true,
      types: true,
      memberVariables: true
  }))
  .pipe(rename({ extname: '' }))
  .pipe(gulp.dest('build'))
})

我首先编译到 ES6,并在导致 typescript 后使用 traceur 显示更少的错误。 我显示 shell 日志:

[19:23:37] Using gulpfile ~/front/gulpfile.js
[19:23:37] Starting 'dependencies'...
[19:23:37] Finished 'dependencies' after 15 ms
[19:23:37] Starting 'ts'...
[19:23:37] Finished 'ts' after 5.96 ms
[19:23:37] Starting 'html'...
[19:23:37] Finished 'html' after 1.15 ms
[19:23:37] Starting 'default'...
[19:23:37] Finished 'default' after 3.13 μs
app/app.ts(1,28): error TS2307: Cannot find module 'angular2/platform/browser'.
app/components/component.ts(1,25): error TS2307: Cannot find module 'angular2/core'.
[19:23:39] TypeScript: 2 semantic errors
[19:23:39] TypeScript: emit succeeded (with errors)

我想知道如何包含打字稿定义文件。 我尝试将此文件添加到 gulp.src 中,例如:

gulp.src(['app/**/*.ts', 'node_modules/angular2/**/*.d.ts)

但显示:重复定义文件错误。

我什至尝试向 package.json 添加输入选项,例如:

"typings": 'node_modules/angular2/platform/browser.d.ts'

但是这个文件有没有递归检测到的导入。

那么...有什么建议吗?

angular 的最后一个版本包括 angular2.d.ts 但不是这个。我不明白为什么我应该使用 typescript 定义文件,无论 angular 是用 typescript 编写的。

是否可以从 node_modules 导入 angular typescript 并通过 typescript 编译器进行编译?

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    您需要在 gulp 文件的 ts 函数中使用 moduleResoultion: "node"。 这将告诉 TypeScript 编译器在 node_modules/angular2/... 中查找定义

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多