Learn TypeScript 3 by Building Web Applications
上QQ阅读APP看书,第一时间看更新

Building from the Terminal

As you might guess, you can also easily build your project both in one-shot and in watch mode from the Terminal.

To build the project once, simply execute npm run tsc, as we saw before.

To build the project in watch mode, you can run npm run tsc -- --watch. Again, alternatively, you can execute npx tsc --watch. Actually, automating things will make your life easier, so why not add a new script to package.json?

Your script block should now look like this:

"scripts": { 
    "tsc": "tsc", 
    "watch": "tsc --watch" 

}, 

With that in place, you should be able to start tsc in watch mode and get the compilation results right from the Terminal:

[20:41:20] Starting compilation in watch mode... 
[20:41:21] Found 0 errors. Watching for file changes. 
... 

If you modify the code and break it, then you'll directly see the results in the console:

[20:41:31] File change detected. Starting incremental compilation... 

todo-it.ts:2:17 - error TS1136: Property assignment expected. 
2 let todoList = {;                   ~ 

Now, you have seen two ways to continuously compile your code. It's up to you to decide which approach you prefer.