Sub topic 8
Strict mode and why it changes your errors
Assume you know JavaScript functions, objects, arrays, modules, and basic TypeScript annotations, interfaces, and union types. This chapter focuses on how compiler options change the diagnostics TypeScript reports.
What strict mode does
TypeScript's strict option enables a group of checks that reject code when its types are uncertain or unsafe. It does not change JavaScript runtime behavior. It changes what the compiler accepts before the program runs.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noEmit": true
},
"include": ["src"]
}The most visible checks include:
| Option | What it checks | Typical error |
|---|---|---|
| `strictNullChecks` | `null` and `undefined` are separate types | Possibly undefined |
| `noImplicitAny` | Parameters need a known type | TS7006 |
| `strictFunctionTypes` | Function parameter types are checked carefully | Assignability errors |
| `strictPropertyInitialization` | Class fields must be initialized | TS2564 |
The exact set can change between TypeScript versions, so check the compiler version when comparing diagnostics.
`strictNullChecks`: missing values are real values
Without strictNullChecks, TypeScript often treats null and undefined as assignable to other types. That hides cases where a value might not exist.
With strict checking enabled, methods such as find return a union type. A union type means the value can be one of several types.
const names: string[] = ["ada", "alan"];
const first = names.find((name) => name.startsWith("a"));
if (first !== undefined) {
console.log(first.length);
} else {
console.log("no match");
}Here, first has the type string | undefined. The if statement narrows the type to string inside its first branch. Narrowing is the process of using a runtime check to remove impossible types.
You can also return a fallback:
const firstName = names.find((name) => name.startsWith("a")) ?? "unknown";
console.log(firstName.length);The ?? operator uses the value on the right when the value on the left is null or undefined.
`noImplicitAny`: type untyped inputs
An implicit any occurs when TypeScript cannot infer a parameter's type and silently gives it any. The any type disables many checks.
function greet(name) {
return `Hello, ${name}`;
}With noImplicitAny, this produces TS7006. Add the type at the boundary:
function greet(name: string): string {
return `Hello, ${name}`;
}This makes callers provide a string and lets the compiler check operations on name.
Choosing strictness for a project
Use strict: true for new projects when possible. If an existing project has many errors, enable options incrementally, fix one category, and then enable the next. Avoid disabling strict mode merely to make the error list shorter. The errors identify assumptions that the code should make explicit.
Exercise
Why does names.find(...) require a check before you access .length when strictNullChecks is enabled?
Show answer
find can return undefined when there is no match, so the result has type string | undefined. A check or fallback narrows it to a usable string.