The compiler and runtime
Create a tsconfig.json
Define the project boundary and compiler behavior once instead of passing unrelated flags on every command.
Without a configuration file, every tsc command needs the same flags repeated, and your editor has no way to know which rules apply to your files. A tsconfig.json fixes both problems. It marks the root of a TypeScript project and records the compiler settings once.
You can generate a starting file:
npx tsc --init
This produces a tsconfig.json full of commented-out options. It works, but I prefer starting from a small explicit file I can read in ten seconds:
{
"compilerOptions": {
"strict": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
},
"include": ["src/**/*.ts"]
}
Each part has a job. include (and exclude, if you add it) chooses the source files. compilerOptions controls checking and output. target decides which JavaScript syntax the compiler may emit. module and moduleResolution decide how imports are written and how they are resolved.
The right module settings depend on where the code runs. NodeNext is right for modern Node.js. A browser bundle built with Vite usually wants "module": "ESNext" with "moduleResolution": "bundler". Do not copy these two lines blindly from a Node.js app into a browser project or the other way around.
Verify the project boundary
Run the compiler with no file arguments:
npx tsc --noEmit
With no files on the command line, tsc walks up from the current directory, finds tsconfig.json, and checks exactly the files matched by include. No output means every file passed.
Be careful with passing individual files. npx tsc src/index.ts ignores your tsconfig.json entirely and falls back to default compiler options. It can pass while the real project check fails, or fail on a rule you never enabled.
When a file seems ignored
If the checker seems to skip a file, it is almost always outside the include patterns. Ask the compiler what the project actually contains:
npx tsc --noEmit --listFiles
The output lists every file in the compilation, including the library declaration files TypeScript ships with. If your file is missing from the list, fix the glob, not the file. A .ts file sitting at the project root is not matched by src/**/*.ts, and that trips people up more than any compiler option.
Keep tsconfig.json in version control. Your editor, your local checks, and continuous integration then all agree on what “correct” means.
Try this: create one .ts file inside src and another outside it, run npx tsc --noEmit --listFiles, and check which of the two shows up.
Lesson completed