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?
相关问题
- Angular RxJS mergeMap types
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
Technically speaking you have a few options here:
If you are using an IDE like Sublime Text and integrated MSN plugin for Typescript: http://blogs.msdn.com/b/interoperability/archive/2012/10/01/sublime-text-vi-emacs-typescript-enabled.aspx you can create a build system which compile the
.ts
source to.js
automatically. Here is the explanation how you can do it: How to configure a Sublime Build System for TypeScript.You can define even to compile the source code to destination
.js
file on file save. There is a sublime package hosted on github: https://github.com/alexnj/SublimeOnSaveBuild which make this happen, only you need to include thets
extension in theSublimeOnSaveBuild.sublime-settings
file.Another possibility would be to compile each file in the command line. You can compile even multiple files at once by separating them with spaces like so:
tsc foo.ts bar.ts
. Check this thread: How can I pass multiple source files to the TypeScript compiler?, but i think the first option is more handy.tsc 0.9.1.1 does not seem to have a watch feature.
You could use a PowerShell script like the one:
Ref: Automatically watch and compile TypeScript files.
Create a file named
tsconfig.json
in your project root and include following lines in it:Please note that
outDir
should be the path of the directory to receive compiled JS files, androotDir
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 insrc
directory into.js
and store them ints-built
directory.The tsc compiler will only watch those files that you pass on the command line. It will not watch files that are included using a
/// <sourcefile>
reference. If your working with the bash, you could use find to recursively find all*.ts
files and compile them:you can watch all files like this
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
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