TypeScript编译器不使用tsconfig.json文件?

4
我是TypeScript的新手。我正在尝试在WebStorm中设置它的使用。 我已经在项目的根目录下创建了一个tsconfig.json文件,并将内置的ts编译器更改为1.6.2版本。 但仍然需要在每个ts文件中包含引用路径。我希望一旦定义了tsconfig.json文件,就不再需要这样做。
我尝试分离问题并在WebStorm之外进行测试。问题仍然存在。 这是我的设置: 我使用“npm install -g typescript”安装了TypeScript。 我创建了一个具有此结构的文件夹:
test\
  tsconfig.json
  src\
     file1.ts
     file2.ts

file2.ts使用了在file1.ts中创建的类。
当我在src文件夹内运行“tsc file2.ts”时,出现了以下错误:
C:\data\tryout\tsconfig\src\file2.ts(11,20): error TS2095: Could not find symbol 'TodoCtrl'.

我需要做什么才能让编译器自动找到所有的ts文件?
file1.ts:
module todos {
    'use strict';

    export class TodoCtrl {
        constructor() { }

        onTodos() {
                console.log('ok');
        }
    }
}

file2.ts:

// does work with ///<reference path='file1.ts' />
module todos {
    'use strict';

    export class DoneCtrl {
        constructor() { }

        onDone() {
            var test = new TodoCtrl();
        }
    }
}

结果:
error TS2095: Could not find symbol 'TodoCtrl'.

我已将所有内容打包成zip文件: https://www.dropbox.com/s/o4x52rddanhjqnr/tsconfig.zip?dl=0
2个回答

3

运行 tsc src\file2.ts 只会使用 src\file2.ts 作为输入。

在没有输入文件的目录中运行 tsc,以便使用 tsconfig.json,并将目录及其子目录中的所有 .ts 文件作为输入。

至于为什么 tsc src\file2.ts 不起作用。您的引用缺少了斜杠。

// <reference path='file1.ts' />

应该是:

/// <reference path='file1.ts' />


我使用 // 将引用放在注释中。它可以与引用一起工作,但我不想使用引用。 - mvermand
如果我在源文件夹中不带任何参数发出tsc命令,我只会得到tsc的用法信息,没有任何编译。 - mvermand
我下载了你的ZIP文件。里面只有两个斜杠。 - thoughtrepo
1
运行 tsc -v。我猜你调用的是旧版的tsc.exe,而不是来自npm的tsc.cmd - thoughtrepo

1

我不确定这对你的工作是否有关,但是你的zip文件包含一个空的tsconfig.json文件。我填写了一些选项,实际上可以从命令行和编辑器编译。

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "rootDir": ".",
        "outDir": ".",
        "sourceMap": false
    },
    "filesGlob": [
        "./src/*.ts"
    ]
}

我希望这能引导你去某个地方。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接