tsconfig Compiler Flags
Dialing the compiler's strictness.
The Rulebook
The tsconfig.json file is the heart of a TypeScript project. It tells the compiler how strict to be and how to emit JavaScript.
The most important flag is "strict": true, which enables a suite of type-checking rules that make TypeScript actually useful.
The Billion Dollar Mistake
Without strict mode, TypeScript allows variables to be null or undefined implicitly. This means you might try to call .toUpperCase() on an undefined string, causing a runtime crash.
By enabling strictNullChecks (which is included in strict), the compiler forces you to handle the null case before accessing properties.
Syntax
json
// tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"strict": true,
"exactOptionalPropertyTypes": true,
"noImplicitAny": true
}
}
Try it
Toggle the strictNullChecks flag in the virtual tsconfig.json and watch how the compiler instantly reacts to unsafe code.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Always turn on
"strict": truefor new projects. strictNullChecksforces you to handlenull/undefinedcases.noImplicitAnyforces you to explicitly type arguments.tsconfig.jsondefines the rules of engagement between you and the compiler.
Done with this concept?
Mark it complete to track your progress. No login needed.