Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch and compile all TypeScript sources?

I'm trying to convert a pet project to TypeScript and don't seem to be able to use the tsc utility to watch and compile my files. The help says I should use the -w switch, but it looks like it can't watch and compile all *.ts files in the some directory recursively. This seems like something tsc should be able to handle. What are my options?

like image 463
VoY Avatar asked Oct 09 '12 11:10

VoY


People also ask

How do I compile all files in TypeScript?

Open a terminal and run tsc -w , it'll compile any . ts file in src directory into . js and store them in ts-built directory.

Do you have to compile TypeScript everytime?

As we know, after writing any TypeScript file, we need to compile it every time and based on that, the . js file will be generated. So, every time, we should compile the TypeScript file by writing the command in the console.

Which programs can be used to automatically compile TypeScript?

ts node - Automatically compile TypeScript files on file save - Stack Overflow.

Which command is used to compile TypeScript?

As you know, TypeScript files can be compiled using the tsc <file name>. ts command.


2 Answers

Create a file named tsconfig.json in your project root and include following lines in it:

{     "compilerOptions": {         "emitDecoratorMetadata": true,         "module": "commonjs",         "target": "ES5",         "outDir": "ts-built",         "rootDir": "src"     } } 

Please note that outDir should be the path of the directory to receive compiled JS files, and rootDir should be the path of the directory containing your source (.ts) files.

Open a terminal and run tsc -w, it'll compile any .ts file in src directory into .js and store them in ts-built directory.

like image 52
budhajeewa Avatar answered Sep 21 '22 04:09

budhajeewa


TypeScript 1.5 beta has introduced support for a configuration file called tsconfig.json. In that file you can configure the compiler, define code formatting rules and more importantly for you, provide it with information about the TS files in your project.

Once correctly configured, you can simply run the tsc command and have it compile all the TypeScript code in your project.

If you want to have it watch the files for changes then you can simply add --watch to the tsc command.

Here's an example tsconfig.json file

{
"compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false
},
"include": [
    "**/*"
],
"exclude": [
    "node_modules",
    "**/*.spec.ts"
]}

In the example above, I include all .ts files in my project (recursively). Note that you can also exclude files using an "exclude" property with an array.

For more information, refer to the documentation: http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

like image 26
dSebastien Avatar answered Sep 21 '22 04:09

dSebastien