引用我的TypeScript模块TS2307时出错:找不到模块

时间:2016-01-06 09:38:10

标签: typescript gulp aurelia gulp-watch

我正在使用aurelia-typescript-skeleton作为我的新项目的基础。我尝试在hello.ts文件夹

中添加新的src文件
export class Hello {
  sayHello(name:string) : string {
    return 'Hello ' + name;
  }
}

并在与下面相同的文件夹中的另一个文件中引用它

import {Hello} from './hello';

export class Users {
  constructor() {
    console.log(new Hello().sayHello('Test'));
  }
}

两个文件都在同一文件夹级别。当我第一次建造时,一切都很好。当我在users.ts文件上进行任何后续更改时,gulp-typescript编译会因为我无法理解的错误而失败。 typescript编译器的错误是

> Starting 'build-system'...
> src\users.ts(4,21): error TS2307: Cannot find module 'hello'.
> TypeScript: 1 semantic error
> TypeScript: emit succeeded (with errors)
> Finished 'build-system' after 950 ms

每当我做一个新的gulp watch时,都没有错误。编辑/更改users.ts文件时出现错误。任何人都可以帮我理解这个错误吗?它必须是基本的......

我在Windows 7环境中,我在两台计算机上收到此错误。

更新:

这是repo to reproduce the problem。重现的步骤:

  1. 克隆存储库,安装所有npmjspm依赖项。
  2. 运行gulp watch - >我没有错误
  3. 更改users.ts文件并保存 - >发生错误。
  4. UPDATE2:

    clean之前添加build-system步骤有助于避免此问题。这是the link to commit。我仍然不确定问题的实际原因。

1 个答案:

答案 0 :(得分:0)

这是因为只有users.ts(已更改的文件)被发送到后续的TS编译,而noResolve已启用(在tsconfig.json中)。

请参阅文件build/tasks/build.js,任务'build-system'

.pipe(changed(paths.output, {extension: '.js'}))

通过比较输入文件的上次修改时间与目标,确定(通过gulp-changed)更改了哪些文件。因此,当您运行涉及clean的{​​{1}}或watch时,目标将被清除,所有文件将再次发送到编译,因此没有错误。

当我尝试更新的骨架应用程序(1.0.0-beta.1.2.2)时,此问题已修复。