Typescript - Can Tsc Be Run Against an Entire Folder?

I'm pretty surprised this isn't in the docs that I could find - but is there any way to just tell tsc to run against all files in a directory and its children without going through a whole tsconfig.json setup?

1

7 Answers

Not that I know of. Using tsc from the command line without a tsconfig.json requires that you specify each file individually, like so:

tsc MyFile1.ts MyFile2.ts MyFile3.ts

However, it looks like you can simply create an empty tsconfig.json (i.e. just {}) at the root of your TypeScript directory and it will do what you want. From :

If no "files" property is present in a tsconfig.json, the compiler defaults to including all TypeScript (*.ts or *.tsx) files in the containing directory and subdirectories.

2

You can use globs

tsc x/file1.ts x/file2.ts x/file3.ts

should be equivalent to

tsc x/*.ts
1

If you have tsconfig.json file inside your project folder, you can directly type

tsc this command will compile all your ts files within the current folder, if you don't have tsconfig.json file, you can generate it by typing:'

tsc -init

This can be overkill, but I just wrote a PS function to use in VS Code as a task;

function CompileTypeScriptFiles($folder) {
    $tsFiles = Get-ChildItem $folder -Filter "*ts" -Recurse
    $tsFiles | ForEach-Object {
        $tsFile = $_.FullName;
        $options = $tsFile + " --outDir js --sourceMap"
        Start-Process "tsc" $options 
    }
}

Windows users need to use for loop:

for %f in (./path/*.ts) do npx tsc "./path/%f" --lib es2018 --outDir ./path/bin

Don't forget to double % if using it inside of bat file:

for %%f in (./path/*.ts) do npx tsc "./path/%%f" --lib es2018 --outDir ./path/bin

You can build all files such as:

tsc ./**/*.ts
1

I got around this on Linux/Mac by running:

pushd dir && tsc && popd

Looks like there needs to be a tsconfig.json file in the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest