Can I run tsc on a specific file and automatically include tsconfig.json?

234 Views Asked by At

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.json file 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 a tsconfig.json file, or a path to a valid .json file containing the configurations.

When input files are specified on the command line, tsconfig.json files 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
1

There are 1 best solutions below

0
VonC On

When you run npx tsc -p tsconfig.json, TypeScript compiles all files in the context of tsconfig.json, applying the configurations specified in there. That is why you see two errors, since the strict option in tsconfig.json enforces 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 only index.ts, without considering tsconfig.json. That is the reason you see only one error; the strict option is not applied.

Currently, there is no direct way to run tsc on a single file and have it automatically include the settings from tsconfig.json. When you specify a file, tsc bypasses the project context created by tsconfig.json.

One workaround is to use project references or a build script that includes tsconfig.json and the specific file you want to compile. For instance, you can create a script that alters tsconfig.json to include only index.ts in the "files" array before running tsc.

And you should also update index.ts to fix the errors:

interface Bar {
    buzz?: number;
}

function foo(bar: Bar) {
    bar.buzz = 100;
}

let obj = { x: 0, bar: 100 };