How to compile a specific file with tsc using the paths compiler option

Specifying the input files on the command line voids your tsconfig.json.

When input files are specified on the command line, tsconfig.json files are ignored.

From the TypeScript docs.

So, there’s no other option than to use a tsconfig.json with the proper ‘include’ (or exclude) specification of the files. The good news is that in the same docs you will find:

A tsconfig.json file can inherit configurations from another file using the extends property.

So what you can do is overwrite the include property of your tsconfig with something like this:

{
  "extends": "./tsconfig.json",
  "include": [
      "src/root/index.ts"
  ]
}

So you could make a script that:

  1. Creates a temporary ‘extends tsconfig’ file with the desired ‘include’ property
  2. Calls tsc with --project specified as that temporary tsconfig

One could argue that this is very complicated and question why they made the decision to not support this scenario properly. However, the question remains why one would like to compile just one file of a project. While developing, you can use the watch (-w) option to compile changes files, and upon building a full project you’ll probably want to compile exactly that, the full project.

Leave a Comment