9

Using

"@angular/cli": "^1.0.0"

@angular/cli ignores tsconfig.json

The project compiles just fine editing / removing tsconfig.json

Why is that?

zurfyx
  • 31,043
  • 20
  • 111
  • 145

2 Answers2

8

The reason that your project compiles without $ROOT/tsconfig.json is that @angular/cli supports multiple apps within the root directory; your (single) app is compiling with $ROOT/src/tsconfig.app.json and your test suite with $ROOT/src/tsconfig.spec.json. The root tsconfig.json applies to all apps within the directory. Deleting it simply removed the global tsconfig that could potentially be used with many apps.

For a clearer understanding of this, look at .angular-cli.json. One property, app, supports an array of multiple applications, but the default configuration is for one application alone. As described in @angular/cli/lib/config/schema.json, app supports "properties of the different applications in this project."

"app": [{
  "root": "src"
  ...
  "tsconfig": "tsconfig.app.json",
  "testTsconfig": "tsconfig.spec.json"
}]

Using this array, you could have as many Angular apps as you like, each with its own specific TypeScript settings.

Additionally, @angular/cli's commands support multiple applications with the --app flag. ng serve --app foo will serve the foo app, whereas ng serve --app bar will serve the bar app. This flag also works with ng build, ng e2e, ng eject, ng test, and ng xi18n. To add a new app to your existing $ROOT directory, use ng new.

The root tsconfig is not just for code editors and tslint. It applies globally to all apps in your project. How this works under the hood is rather complex, as is clear from the source code, where there are numerous json files that have lines such as "extends": "../../../tsconfig.json".

jfmercer
  • 3,641
  • 3
  • 29
  • 35
  • 1
    Very helpful. Due to the custom name of `tsconfig.app.json` do you know if it is possible to manually compile with a tsc command from root that emulates ng build? – kampsj Jul 14 '17 at 13:09
7

As stupid as it may seem, the angular-cli 's tsconfig.json file is located inside src/tsconfig.app.json

The root tsconfig.json will be used by editors (such as vscode).

zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • you can tell your editor to use the `src/tsconfig.app.json` why have two separate ones? – Ahmed Musallam Apr 01 '17 at 05:37
  • @AhmedMusallam angular-cli seems to create two of them by default – zurfyx Apr 01 '17 at 07:00
  • Actually 3 of them in angular-cli 1.0.0 for app, e2e and test i believe. Regardless, my point was about creating 2 tsconfig files for app. – Ahmed Musallam Apr 01 '17 at 15:43
  • @AhmedMusallam I'm not sure why Angular team did it this way. But, you generally want to have different config files if you share different config. In this case, `src/tsconfig.app.json` would be more specific configuration. This answer's intention isn't to change what they did though, but rather to point out the right configuration file. – zurfyx Apr 01 '17 at 16:05
  • you can have as many as you want. As long as you can justify it. In their case i believe it has to do with how they transpile .ts test files vs app files. – Ahmed Musallam Apr 01 '17 at 16:09