21

Normally I run tsc -p ./tsconfig.json which type checks all files in the ./src folder relative to tsconfig.

But if I run tsc -p ./tsconfig.json src/specific-file.ts, it complains

error TS5042: Option 'project' cannot be mixed with source files on a command line.

So, if I remove the option and run tsc src/specific-file.ts then it checks the file, but it does not use any settings from tsconfig (because I haven't specified the tsconfig file?).

How can I run tsc on a single file and using the settings in tsconfig that would otherwise be used on the whole project?

trusktr
  • 44,284
  • 53
  • 191
  • 263

2 Answers2

5

I don't know of a really good solution short of writing your own command-line tool using the TypeScript compiler API. Two easier approaches you might consider:

  1. Write a script that generates a temporary tsconfig.json file that extends your original tsconfig.json and sets files to just the file you want. However, if other files contain global declarations that are needed to type check the file you specified, the other files may not get loaded, so this approach may not work.

  2. Write a script that runs tsc on the entire project and filters the output as shown in this answer. However, if your concern was performance, this won't help.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
  • 3
    That's basically what I was thinking, and yep my concern was performance for a git precommit hook to enforce typing. For now, I'm just type checking the whole project, but I figure if I commit a single file, I only need to check it (and it's dependency graph and global types). – trusktr Nov 02 '18 at 01:15
  • Determining which files could potentially have errors introduced by a change to a given file is not an easy task. – Matt McCutchen Nov 02 '18 at 01:18
  • Good point. We would need to traverse up the dependency tree in that case, and how would we do that? By traversing down the whole project tree in the first place I suppose. (just an idea, but maybe TS can cache a dependency graph in a project or somewhere, in order to make it possible to go upward) – trusktr Nov 02 '18 at 03:56
-3

Check to make sure your absolute path doesn't contain any spaces, and if you still get the error, it may simply be because tsconfig.json is meant for configuring projects, and command-line arguments are meant for files.

Coder-256
  • 5,212
  • 2
  • 23
  • 51
  • There's no spaces. It would be redundant (and possibly error prone) to duplicate the command line args that I'm already using from tsconfig. It'd be nice to be able to re-use the config, except specifying the files at command line (for automation purposes). – trusktr Nov 02 '18 at 01:11