Working With tsc

So far tsc has been used to compile a single file. Typically programmers havea lot more than one file to compile. Thankfully tsc can handle multiple files asarguments.

Imagine two ultra simple files/modules:

a.ts

  1. export const A = (a) => console.log(a);

b.ts

  1. export const B = (b) => console.log(b);

Before TypeScript@1.8.2:

  1. $ tsc ./a.ts ./b.ts
  2. a.ts(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided.

Hmmm. What's the deal with this module flag? TypeScript has a help menu, let'stake a look:

  1. $ tsc --help | grep module
  2. -m KIND, --module KIND Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'
  3. --moduleResolution Specifies module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).

(TypeScript has more help than what we've shown; we filtered by grep for brevity.)There are two help entries that reference "module", and —module is the one TypeScript was complaining about.The description explains that TypeScript supports a number of different module schemes.For the moment commonjs is desirable. This will produce modules that are compatible with node.js's module system.

  1. $ tsc -m commonjs ./a.ts ./b.ts

Since TypeScript@1.8.2, tsc has a default rule for —module option: target === 'ES6' ? 'ES6' : 'commonjs' (more details can be found here), so we can simply run:

  1. $ tsc ./a.ts ./b.ts

tsc should produce no output. In many command line traditions, no output isactually a mark of success. Listing the directory contents will confirm thatour TypeScript files did in fact compile.

  1. $ ls
  2. a.js a.ts b.js b.ts

Excellent - there are now two JavaScript modules ready for consumption.

Telling the tsc command what to compile becomes tedious and labor intensiveeven on small projects. Fortunately TypeScript has a means of simplifying this.tsconfig.json files let programmers write down all the compiler settings theywant. When tsc is run, it looks for tsconfig.json files and uses theirrules to compile JavaScript.

For Angular projects there are a number of specific settings that need to beconfigured in a project's tsconfig.json

  1. {
  2. "compilerOptions": {
  3. "module": "commonjs",
  4. "target": "es5",
  5. "emitDecoratorMetadata": true,
  6. "experimentalDecorators": true,
  7. "noImplicitAny": false,
  8. "removeComments": false,
  9. "sourceMap": true
  10. },
  11. "exclude": [
  12. "node_modules",
  13. "dist/"
  14. ]
  15. }

Target

The compilation target. TypeScript supports targeting different platforms depending on your needs. In our case, we're targeting modern browsers which support ES5.

Module

The target module resolution interface. We're integrating TypeScript through webpack which supports different interfaces. We've decided to use node's module resolution interface, commonjs.

Decorators

Decorator support in TypeScript hasn't been finalized yet but since Angular uses decorators extensively, these need to be set to true. Decorators have not been introduced yet, and will be covered later in this section.

TypeScript with Webpack

We won't be running tsc manually, however. Instead, webpack's ts-loader will do the transpilation during the build:

  1. // webpack.config.js
  2. //...
  3. rules: [
  4. { test: /\.ts$/, loader: 'ts', exclude: /node_modules/ },
  5. //...
  6. ]

This loader calls tsc for us, and it will use our tsconfig.json.

原文: https://angular-2-training-book.rangle.io/handout/features/working_with_tsc.html