【问题标题】:Typescript module is not defined打字稿模块未定义
【发布时间】:2016-09-05 05:45:15
【问题描述】:

我正在升级到 Angular2 RC1,但在一个非常简单的打字稿服务器上突然出现以下错误:

我已经安装了我认为需要的类型,并且正在使用 gulp 构建它: 打字.json:

{
  "ambientDependencies": {
  "es6-collections": "registry:dt/es6-collections#0.5.1+20160316155526",
  "es6-promise": "registry:dt/es6-promise#0.0.0+20160423074304",
  "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654",
  "express": "registry:dt/express#4.0.0+20160317120654",
  "express-serve-static-core": "registry:dt/express-serve-static-core#0.0.0+20160322035842",
  "node": "registry:dt/node#4.0.0+20160505172921",
  "require": "registry:dt/require#2.1.20+20160316155526",
  "serve-static": "registry:dt/serve-static#0.0.0+20160501131543"
  }
}

代理构建的 gulp 任务:

gulp.task('build:proxy', function () {
    var tsProject = ts.createProject('tsconfig.json');
    var tsResult = gulp.src(['proxy/**/*.ts','!proxy/typings/**/*.*'])
        .pipe(sourcemaps.init())
        .pipe(ts(tsProject))
    return tsResult.js
        .pipe(concat('proxy.js'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('dist'))
});

和 tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "proxy/typings/main",
    "proxy/typings/main.d.ts"
  ],
  "buildOnSave": false,
  "disableCompileOnSave": true,
  "compileOnSave" : false
}    

因为一切看起来都像在我保持导入相同之前的样子,例如import express = require('express')。我似乎无法弄清楚我必须做什么才能让这些模块进入。

【问题讨论】:

  • 你检查过typings/main.d.ts吗?
  • 您的源 ts 文件是否驻留在代理\\ts 文件中?
  • @Zen 不错,看起来不错!
  • @MadhuRanjan 代理文件夹内只有一个名为 proxy.ts 的 ts 文件。我也尝试将所有内容移至根文件夹,但同样的问题。

标签: typescript


【解决方案1】:

在代理文件夹中放置一个新的 main.d.ts,内容如下

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

【讨论】:

  • 试过了,我得到了重复的标识符(也尝试在 tsconfig 的类型中排除 main.d.ts 并且仍然得到重复的错误)。
【解决方案2】:

我回答了 GitHub 问题 (https://github.com/typings/typings/issues/491)。总之,您需要更正gulp.src() 调用以不忽略所有 类型,而只忽略main浏览器 类型。例如。 gulp.src(['proxy/**/*.ts', '!proxy/typings/main/**', '!proxy/typings/main.d.ts'].

【讨论】:

猜你喜欢
  • 2016-11-06
  • 2012-11-15
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2016-12-12
相关资源
最近更新 更多