Sub topic 12
Useful Checks That `strict` Does Not Include
This chapter assumes basic TypeScript syntax, JSON configuration, and the ability to run npx tsc. The strict option enables several type-safety checks, but it does not enable every useful compiler check. Some risks need separate options because they depend on project style or runtime behavior.
Checking Indexed Access
By default, TypeScript assumes that an indexed lookup succeeds when the index type is valid:
const items: string[] = ["a", "b"];
const first = items[0];
console.log(first.toUpperCase());At runtime, items[10] is undefined. Enable noUncheckedIndexedAccess to make TypeScript include undefined in the result of an indexed lookup.
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true
}
}Now items[0] has the type string | undefined, so the code must check the value:
const items: string[] = ["a", "b"];
const first = items[0];
if (first !== undefined) {
console.log(first.toUpperCase());
}This option also affects object types with index signatures, such as Record<string, string>. It is useful when keys or array positions come from user input.
Checking Class Overrides
noImplicitOverride requires the override keyword when a subclass replaces a method from its base class.
{
"compilerOptions": {
"strict": true,
"noImplicitOverride": true
}
}class Logger {
write(message: string): void {
console.log(message);
}
}
class FileLogger extends Logger {
override write(message: string): void {
console.log(`file: ${message}`);
}
}The keyword documents that the replacement is intentional. It also catches a renamed or misspelled base method that would otherwise become a new method by accident.
Checking Unused Code and Control Flow
These options find maintenance problems rather than uncertain types:
| Option | What it catches |
|---|---|
| `noUnusedLocals` | Unused variables and imports inside a file |
| `noUnusedParameters` | Function parameters that are never used |
| `noFallthroughCasesInSwitch` | A `switch` case that falls into the next case |
| `allowUnreachableCode: false` | Code that can never execute |
For example, noFallthroughCasesInSwitch can reveal a missing break, return, or intentional fallthrough comment. These checks may produce many errors in an older project, so enable them after reviewing the existing code.
Choosing Extra Checks
A practical configuration often starts with strict type checking and adds checks based on the project:
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
}
}Each option increases feedback from the compiler. The trade-off is that existing code may need more explicit checks, cleanup, or annotations.
Exercise
Why might items[0] have the type string | undefined when noUncheckedIndexedAccess is enabled?
Show answer
The index might be outside the array's current range at runtime, so the lookup may return undefined.