tsconfig, strict mode and compiling
In the previous lesson we checked a single file by passing it to tsc. In a real project you have many files and you need all of them checked with the same rules. Those rules live in a file called tsconfig.json at the project root. When you run tsc with no arguments, the compiler looks for that file and obeys it. Today we create it, understand its main options and turn on strict mode, the most important decision of the whole course.
Creating the tsconfig
You can generate one with npx tsc --init, but it produces a long file full of comments. To learn, it is better to write a small one by hand, where every line has a reason. This is a solid starting point for practicing with Node.js 22.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true
},
"include": ["src"]
}- target: the JavaScript version that is generated. ES2022 is safe on Node 22.
- module and moduleResolution: how import and export are interpreted; NodeNext follows Node's real rules.
- strict: turns on the full set of strict checks.
- outDir and rootDir: where the source code lives and where the compiled JavaScript is written.
- skipLibCheck: does not check the declaration files of libraries, which speeds up compiling.
- include: which folders belong to the project.
What strict changes
strict is not a single rule but a switch that turns on several. The two most influential are noImplicitAny and strictNullChecks. The first forbids TypeScript from accepting an any type when it cannot deduce anything: it forces you to write the type of a parameter that cannot be inferred. The second separates null and undefined from all other types, so a string is never null by accident. With it, the famous class of errors Cannot read properties of undefined mostly disappears. Without strict, TypeScript is far more permissive and loses much of its value.
// src/index.ts
function double(value) {
// error with strict: Parameter 'value' implicitly has an 'any' type
return value * 2;
}
function firstLetter(text: string): string {
return text[0];
}
const names: string[] = [];
const first: string | undefined = names[0];
console.log(first.toUpperCase());
// error with strict: 'first' is possibly 'undefined'Notice the second error: the compiler knows an empty array has no first element and forces you to handle that case before calling toUpperCase. At first these warnings feel like a nuisance; in reality they are real bugs you would have discovered in production. One piece of advice: always start with strict on. Turning it on later in a big project is painful, while starting with it is free.
Compiling and running
npx tsc # checks types and writes dist/index.js
node dist/index.js # runs the compiled JavaScript
npx tsc --noEmit # only checks types, writes nothing
npx tsx src/index.ts # runs directly, no type checking✎ मेरे नोट्स
केवल इस ब्राउज़र में सहेजा गया। खाते में रखने के लिए साइन इन करें।0/5000 अक्षर