tsconfig.json not used by TypeScript compiler?

2019-05-21 02:24发布

问题:

I'm new to TypeScript. I am trying to setup the use of it in WebStorm. I have created a tsconfig.json file in the root of my project and changed the builtin ts compiler to version 1.6.2. Still I need to include reference paths in every ts file. I hoped this would not be needed anymore once I defined the tsconfig.json file.

I tried to isolate the issue and went testing outside WebStorm. The issue remains. This is my setup: I installed TypeScript with "npm install -g typescript". I created a folder with this structure:

test\
  tsconfig.json
  src\
     file1.ts
     file2.ts

file2.ts uses a Class which is created in file1.ts.

When I run "tsc file2.ts" inside the src folder, I get a:

C:\data\tryout\tsconfig\src\file2.ts(11,20): error TS2095: Could not find symbol 'TodoCtrl'.

What do I need to do to get the compiler find all ts files automatically?

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();
        }
    }
}

result:

error TS2095: Could not find symbol 'TodoCtrl'.

I have put everything in a zip: https://www.dropbox.com/s/o4x52rddanhjqnr/tsconfig.zip?dl=0

回答1:

Running tsc src\file2.ts will only use src\file2.ts as the input.

Run tsc in the directory with no input files for it to use the tsconfig.json, and input all .ts files in the directory and subdirectories.

As for why tsc src\file2.ts doesn't work. Your reference was missing a slash.

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

should be:

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



回答2:

I'm not sure if this will be relevant to your work but your zip contains an empty tsconfig.json. I filled it in with some options and actually was able to compile, both from the command line and from the editor.

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

I hope this leads you somewhere.