Background
I have a typescript file and tsconfig.json in the same directory:
index.ts:
function foo(bar) {
bar.buzz = 100;
}
let obj = { x: 0 };
obj.bar = 100;
tsconfig.json:
{
"compilerOptions": {
"strict": true
}
}
If I run npx tsc -p tsconfig.json, I get 2 errors, which is what I expect.
If I run npx tsc index.ts, I only get 1 error.
My Question
Is there a way to run npx tsc index.ts (or similar) and have it pick up the tsconfig file?
If so, how do I do it?
If not, why not? I'm new to typescript and trying to understand how everything is connected.
For what it's worth, from my reviewing of the tsconfig doc, and various other searching around, it seems like you cannot run npx tsc index.ts and have it pick up the tsconfig file:
A project is compiled in one of the following ways:
- By invoking tsc with no input files, in which case the compiler searches for the
tsconfig.jsonfile starting in the current directory and continuing up the parent directory chain.- By invoking tsc with no input files and a
--project(or just-p) command line option that specifies the path of a directory containing atsconfig.jsonfile, or a path to a valid.jsonfile containing the configurations.When input files are specified on the command line,
tsconfig.jsonfiles are ignored.
Nevertheless, it would be useful to have my guess confirmed by someone more experienced.
(Optional-ish: Additional details)
Environment details:
$ node -v
v18.19.0
$ npx tsc -v
Version 5.3.3
OS: MacOS Catalina (10.15.7)
Full tsc command outputs
$ npx tsc index.ts
index.ts:6:5 - error TS2339: Property 'bar' does not exist on type '{ x: number; }'.
6 obj.bar = 100;
~~~
Found 1 error in index.ts:6
$ npx tsc -p tsconfig.json
index.ts:1:14 - error TS7006: Parameter 'bar' implicitly has an 'any' type.
1 function foo(bar) {
~~~
index.ts:6:5 - error TS2339: Property 'bar' does not exist on type '{ x: number; }'.
6 obj.bar = 100;
~~~
Found 2 errors in the same file, starting at: index.ts:1
$ npx tsc
index.ts:1:14 - error TS7006: Parameter 'bar' implicitly has an 'any' type.
1 function foo(bar) {
~~~
index.ts:6:5 - error TS2339: Property 'bar' does not exist on type '{ x: number; }'.
6 obj.bar = 100;
~~~
Found 2 errors in the same file, starting at: index.ts:1
When you run
npx tsc -p tsconfig.json, TypeScript compiles all files in the context oftsconfig.json, applying the configurations specified in there. That is why you see two errors, since thestrictoption intsconfig.jsonenforces stricter type-checking.You can see an example in "Publishing Node modules with TypeScript and ES modules" from Jack Franklin .
But, when you run
npx tsc index.ts, TypeScript compiles onlyindex.ts, without consideringtsconfig.json. That is the reason you see only one error; thestrictoption is not applied.Currently, there is no direct way to run
tscon a single file and have it automatically include the settings fromtsconfig.json. When you specify a file,tscbypasses the project context created bytsconfig.json.One workaround is to use project references or a build script that includes
tsconfig.jsonand the specific file you want to compile. For instance, you can create a script that alterstsconfig.jsonto include onlyindex.tsin the"files"array before runningtsc.And you should also update
index.tsto fix the errors: