Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 460
trusktr Avatar asked Nov 02 '18 00:11

trusktr


People also ask

How is Tsconfig json used?

The tsconfig. json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig. json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

What can be defined in the Tsconfig JSON file?

json file is a file of JSON format which allows us to point the root level files and different compiler options to setup that require to compile a TypeScript based projects. The existence of this file in a project specifies that the given directory is the TypeScript project folder root.

What does the TSC command do?

The tsc command envokes the TypeScript compiler. When no command-line options are present, this command looks for the tsconfig. json file. If no tsconfig.


1 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.

like image 66
Matt McCutchen Avatar answered Oct 04 '22 18:10

Matt McCutchen