Understanding strict Mode
Understanding strict Mode — why it matters
strict enables a coordinated family of stronger TypeScript checks, including nullability and implicit-any checks. It catches many mistakes before running code, but cannot guarantee that untrusted data or runtime dependencies behave correctly. Enabling strict for a new project is usually simpler than converting a large permissive project later.
What you’ll learn
- Explain understanding strict mode and the problem it solves.
- Identify the relevant compile-time checks and runtime responsibilities.
- Run the example, predict its output, and try a realistic modification.
Understand the mechanism
A null guard narrows input to string in the else branch. With strictNullChecks enabled through strict, accessing input.length without the guard would be rejected.
Strict checking does not inspect actual network responses or prevent deliberate unsafe assertions.
Follow the concept flow
Run the primary TypeScript example
Save as strict-mode.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.
TypeScript source
function printLength(input: string | null): string {
return input === null ? "No text" : `Length: ${input.length}`;
}
console.log(printLength("TypeScript"));
console.log(printLength(null));Verified output
Length: 10
No textCompile: npx tsc --strict --target es2022 --module commonjs strict-mode.ts; run: node strict-mode.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.
The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.
Trace the result
A null guard narrows input to string in the else branch. With strictNullChecks enabled through strict, accessing input.length without the guard would be rejected.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Try the full tool or configuration workflow
Practical project lab: These are real configuration and source-file examples, not a TypeScript object that merely describes settings. Place the files at the named paths relative to one project directory.
tsconfig.json
{"compilerOptions":{"strict":true,"noEmit":true}}main.ts
function safe(s: string | null) { return s?.length ?? 0; }
console.log(safe(null));Commands
npx tsc -p .strict enables coordinated strong checks; inspect the compiler diagnostic if the guard is removed.
This local compiler or test-runner mini-project was validated in addition to the primary program.
Common mistake and correction
Strict checking does not inspect actual network responses or prevent deliberate unsafe assertions.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Remove the null guard and inspect the error. Restore it and add an undefined branch.
Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.
Check your understanding
Official reference
For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.
What to remember
strict enables a coordinated family of stronger TypeScript checks, including nullability and implicit-any checks.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.