tsc-with-config v2.0.2
tsc-with-config
tsc-with-config
is a CLI tool that lets you run tsc
with files and tsconfig.json
, which is not allowed by the original tsc
command.
Running tsc locally will compile the closest project defined by a
tsconfig.json
, or you can compile a set of TypeScript files by passing in a glob of files you want. When input files are specified on the command line, tsconfig.json files are ignored. tsc CLI Options
Installation
npm i -D tsc-with-config
or
pnpm add -D tsc-with-config
or
yarn add -D tsc-with-config
Usage
tsc-with-config
exposes a binary tscw
.
npx tscw foo.ts
or
npx tscw *.ts # match ./foo.ts, ./bar.ts ...
or
npx tscw **/*.ts # match ./foo/baz.ts, ./bar/foo.ts ...
!NOTE By default, the nearest
tsconfig.json
is used.tscw
supports all CLI options supported bytsc
.
Use case
A common use case for running tsc
on certain files is when used in a pre-commit hook. e.g. lint-staged.
For example only type-check staged files by running tsc --noEmit foo.ts bar.ts
, in this case tsc
will ignore the tsconfig.json
, using -p tsconfig.json
with files will result in an error.
You can explicitly pass the CLI options in. e.g. --strict --allowSyntheticDefaultImports ...
to tsc
, but that can be tedious.
Using tscw
is much easier: tscw --noEmit foo.ts bar.ts -p tsconfig.json
.
How it works
- CLI Argument Parsing:
- The script processes command-line arguments to handle flags and file paths.
- Finding
tsconfig.json
:- If no
tsconfig.json
file is specified via the-p
or--project
flag, the nearesttsconfig.json
file will be used for the current workspace. - The script first looks for the current working directory, if not found, it goes all the way up until the level where
package.json
is located.
- If no
- Temporary File:
- A temporary file is created to store the content of the
tsconfig.json
file being used. - It adds/replaces the
"files"
field with the files specified. - It empties the
"include"
field.
- A temporary file is created to store the content of the
- Running
tsc
:- It runs
tsc
with the temp file and any specified flags.
- It runs
- Cleanup:
- The script removes the temporary file when the script exits or receives certain signals(SIGINT, SIGHUP, SIGTERM).