Sub topic 8
Turning on strict mode
Assume you know JavaScript functions, objects, modules, and basic TypeScript annotations. You should also have Node.js, npm, and the TypeScript compiler installed. This chapter uses a project configured with tsconfig.json.
TypeScript can infer many types, but it can also allow uncertain code for compatibility with JavaScript. Strict mode changes that default. It asks the compiler to report more situations where your assumptions may be wrong.
Enable it in tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}strict is a group of related compiler checks. You can enable individual checks instead, but strict: true is the usual starting point for a new project.
What strict mode checks
Strict mode catches both missing information and unsafe assumptions. Important checks include:
| Option | Problem it catches |
|---|---|
| `noImplicitAny` | A value has the `any` type because no type was provided |
| `strictNullChecks` | Code uses `null` or `undefined` as if a value always exists |
| `strictFunctionTypes` | A function accepts arguments that are too narrow |
| `strictPropertyInitialization` | A class property may be read before construction initializes it |
| `useUnknownInCatchVariables` | Caught errors are treated as safely known values |
The exact set can grow as TypeScript adds checks. Individual options can override the group:
{
"compilerOptions": {
"strict": true,
"strictNullChecks": false
}
}This weakens the project, so use overrides as temporary migration steps rather than as a permanent way to avoid errors.
The most visible check: possibly missing values
With strictNullChecks, undefined and null are separate types. A function that may not receive text must say so:
function firstLetter(text: string | undefined): string {
if (text === undefined) {
return "";
}
return text.charAt(0);
}
console.log(firstLetter("Ada"));
console.log(firstLetter(undefined));The | symbol creates a union type. string | undefined means the value can have either type. TypeScript narrows the type inside the if statement, so text.charAt(0) is safe afterward.
Without the check, this code appears valid:
function firstLetter(text: string | undefined): string {
return text.charAt(0);
}It can fail at runtime when text is undefined. Strict mode moves that discovery to compilation.
Fixing strict errors systematically
When strict mode reports an error, follow the value back to its source.
- Identify the value that may be missing or incorrectly typed.
- Decide whether the value is truly optional.
- Represent that possibility in the type.
- Handle every allowed case before using the value.
- Run the compiler again.
For an existing JavaScript project, turn checks on gradually if the error count is large. You can start with strict: false, enable one option, fix its errors, and then enable the full group. For a new TypeScript project, enable strict mode immediately. Early errors are cheaper to fix because fewer files depend on the uncertain code.
How strict mode fits the compiler workflow
Strict mode affects type checking, not the JavaScript runtime. The compiler checks the source and then removes TypeScript-only types during emit.
flowchart TD
A[TypeScript source] --> B[Strict type checking]
B -->|errors| C[Fix types or control flow]
C --> B
B -->|passes| D[Emit JavaScript]
D --> E[Run with Node.js]Caption: Strict mode checks assumptions before JavaScript is emitted. Alt: A flowchart showing TypeScript source entering strict checking, returning errors for fixes, or continuing to JavaScript output and then Node.js.
Use the compiler to verify the project:
npx tsc --noEmitThe --noEmit flag performs strict type checking without writing JavaScript files. This is useful in continuous integration, where another tool handles the build.
Exercise
Why does strict mode reject return text.charAt(0) when text has type string | undefined, and what should you do instead?
Show answer
The value may be undefined, which has no charAt method. Check for undefined, provide a fallback, or change the type only if the value is guaranteed to exist.