2

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.

9
  • Your out is wrong. It should be just "outFile": "main.js". Try to add export` in front of module. Commented Nov 23, 2015 at 15:01
  • Also, the compiler still creates a "car.js" file, which should not be necessary? Commented Nov 23, 2015 at 15:03
  • 1
    Your editor may create car.js but that's not the main problem. Please post the content of generated main.js too. Commented Nov 23, 2015 at 15:04
  • Hey, it seems to be working now! I can finally include internal modules from separate files. Thanks for all the help! Apparently, I can even leave out the 'files' option in tsconfig.json. The only problem now is that the TS files are not yet concatenated into one big JS file. Commented Nov 23, 2015 at 15:16
  • 1
    Yes, files is not necessary. I don't use the option. We use gulp-typescript for compiling .ts files because we can compile incrementally the code then which is faster. Commented Nov 23, 2015 at 15:18

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

You missed the Vehicles module
The Typescript Handbook literally states that you can concatenate multiple .ts files into one .js file using the --out flag. This part is still not working in VS Code with the above config file. Actually, the "out" flag is completely ignored!
the --out flag is for use in console, don't expect it to work in tsconfig.json someone hinted to use outFile, did you try this? btw. why don't you use Visual Studio 2015?
Yup, also tried outfile. What's confusing about this is that microsoft's own Typescript documentation does not seem to apply to their own product VS Code... :( I do like VS Code though! It's fast and clean. It's just hard to configure. VS Studio is not available for mac. Also I find that tool somewhat bloated for these simple purposes.
oh a mac, ok. But yeah i agree its a nice little Programm. According to: code.visualstudio.com/docs/languages/typescript you can have a "tasks.json" file, with commands and arguments
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.