strictNullChecks
strictNullChecks — why it matters
With strictNullChecks enabled, null and undefined are not automatically assignable to arbitrary types. Return types from lookups accurately include a possible missing value, which you must handle before dereferencing. Use a guard or explicit fallback rather than a blind non-null assertion.
What you’ll learn
- Explain strictnullchecks 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
Map.get returns number | undefined. The strict equality check removes undefined so the formatting branch can safely use the number.
The postfix ! only silences a compiler warning; it does not protect you from undefined at runtime.
Follow the concept flow
Run the primary TypeScript example
Save as strictnullchecks.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
const prices = new Map<string, number>([["book", 25]]);
function priceOf(key: string): string {
const price = prices.get(key);
return price === undefined ? "Not found" : `$${price}`;
}
console.log(priceOf("book"));
console.log(priceOf("pen"));Verified output
$25
Not foundCompile: npx tsc --strict --target es2022 --module commonjs strictnullchecks.ts; run: node strictnullchecks.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
Map.get returns number | undefined. The strict equality check removes undefined so the formatting branch can safely use the number.
Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.
Common mistake and correction
The postfix ! only silences a compiler warning; it does not protect you from undefined at runtime.
Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.
Practice the concept
Change the lookup to return number | null and explain how callers must narrow it.
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
With strictNullChecks enabled, null and undefined are not automatically assignable to arbitrary types.
Run the primary example, then complete the practice task or environment-specific lab.
Reaching the recap and completing the quizzes are separate milestones.