How to type check a single file from command line, using the setting in tsconfig.json?

Viewed 9294

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?

2 Answers

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.

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.

Related