Kokodoko
Kokodoko

Reputation: 28128

Typescript internal module not recognized by VS Code

I am trying to separate my typescript classes in separate files using internal modules. However, the main.ts file will not load or recognize the sub modules.

main.ts

/// <reference path="Car.ts" />
module Vehicles {
    var c = new Vehicles.Car("red");
} 

car.ts

module Vehicles {
    export class Car {
        color: string;
        constructor(color: string) {
            this.color = color;
            console.log("created a new " + color + " car");
        }
    }
}

tsconfig.json

{
    "compilerOptions": {
        "sourceMap":  true, 
        "out": "everything.js main.ts car.ts"
    }
}

Update: edited the "out" flag in tsconfig to try and compile main.ts and car.ts into everything.js - this is the last part that is not working: everything.js is not created. Instead, VS Code creates a main.js and a car.js. It seems that the "out" flag is ignored. I have also tried "outFile" with the same result.

Upvotes: 2

Views: 505

Answers (2)

CoderPi
CoderPi

Reputation: 13211

main.ts

/// <reference path="car.ts" />
var c = new Car("red");

car.ts

class Car {
    color: string;
    constructor(color: string) {
        this.color = color;
        console.log("created a new " + color + " car");
    }
}

tsconfig.json

{
    "compilerOptions": {
        "sourceMap":  true, 
        "outFile": "main.js"
    },
    "files": [
        "main.ts",
        "car.ts"
    ]
}

tasks.json

Kokodoko: I finally found the problem! You have to OMIT the "args" option inside "tasks.json", only then will the arguments in tsconfig.json be used! I found the answer here: github.com/Microsoft/typescript/wiki/tsconfig.json. It says: When input files are specified on the command line, tsconfig.json files are ignored


For further information about Modules, don't forget to have a look at the TypeScript Handbook

Upvotes: 2

Kokodoko
Kokodoko

Reputation: 28128

To compile several .ts files into one big .js file using a VS Code task, you need to remove the 'args' from tasks.json and add the "out" argument to tsconfig.json

tasks.json

{
    "version": "0.1.0",
    "command": "tsc",
    "isShellCommand": true,
    "showOutput": "silent",
    "problemMatcher": "$tsc"
}

tsconfig.json

{
    "compilerOptions": {
        "sourceMap":  true,
        "out": "myapp.js"
    }
}

Note: When input files are specified on the command line, tsconfig.json files are ignored.

Upvotes: 1

Related Questions