Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output folder for compiled TypeScript

I'm following a tutorial called THE HERO EDITOR, and it has me creating and editing TypeScript files in an app folder. The app uses a script tsc -w, where the w causes the tsc transpiler to output a new JavaScript file every time a TypeScript file fails.

This caused me some confusion for several minutes, because when I added a TypeScript class, the editor (Visual Studio Code) underlined the class name and told me it was a duplicate declaration. I saw the JavaScript file with the same name, but no sooner had I deleted it, the duplicate declaration, and thus the JS file, were back.

The script is declared in package.json:

"scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
},

Where can I specify where compiled JavaScript goes? I doubt I can do it here, but I can only guess that I need a grunt or equivalent file, but then what do I do in that file, and do I remove the tsc entries from `package.js' once another file takes over?

like image 520
ProfK Avatar asked Aug 23 '16 08:08

ProfK


People also ask

Where does compiled TypeScript go?

Since the TypeScript compiler preserves the input file path, it will generate x. js in the parent directory of the root directory.

Does tsc use Tsconfig?

TypeScript compiler uses tsconfig. json to get configuration options for generating JavaScript code from TypeScript source-code. When you uses '$ tsc' command to compile TypeScript code, compiler searches for configurations located in tsconfig. json .

What does tsc do in TypeScript?

Tsc stands for `TypeScript compiler` and is a simple tool included in Typescript itself, allowing you to compile any ts files into js.

What is Rootdir in Tsconfig?

Default: The longest common path of all non-declaration input files. If composite is set, the default is instead the directory containing the tsconfig. json file. When TypeScript compiles files, it keeps the same directory structure in the output directory as exists in the input directory.


1 Answers

We can control where our compiled javascript goes with the help of "outDir" parameter of "compilerOptions" in our tsconfig.json file.

I am also using Visual Studio Code and I generally create my tsconfig.json file at the project root directory. Here is an example, where I want my compiled javascript to go inside "src/ts-out/" directory

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "outDir": "src/ts-out/"
    }    
}

Hope it helps. Happy coding...

like image 85
Suman Barick Avatar answered Oct 12 '22 13:10

Suman Barick