Sub topic 12
Useful checks that strict does not include
This chapter assumes you can read a tsconfig.json, know that "strict": true is an umbrella option, and can fix basic noImplicitAny and strictNullChecks errors. Those flags are covered in earlier chapters.
Why some checks sit outside strict
The strict flag groups the checks the TypeScript team considers safe to enable together. Other checks stay outside it for two reasons. Some produce many warnings on ordinary code, such as unused variables. Others change how you write common patterns, such as array indexing. Because they are not part of strict, you must enable each one explicitly, and strict will not turn on new ones when you upgrade TypeScript. Adding strict to a project also does not give you any of these checks.
The table lists the checks this chapter covers.
| Flag | What it catches |
|---|---|
| `noUncheckedIndexedAccess` | Index reads that may return `undefined` |
| `noImplicitOverride` | Overriding a base method without saying so |
| `exactOptionalPropertyTypes` | Assigning `undefined` to an optional property |
| `noImplicitReturns` | Code paths that forget to return a value |
| `noFallthroughCasesInSwitch` | Accidental fallthrough between `case` clauses |
| `noPropertyAccessFromIndexSignature` | Dot access on keys that come from an index signature |
| `noUnusedLocals`, `noUnusedParameters` | Declared but unused variables and parameters |
noUncheckedIndexedAccess
By default, reading items[0] from a string[] gives type string, even when the array is empty. With noUncheckedIndexedAccess, the type becomes string | undefined, and you must check it first.
const items: string[] = ["a", "b"];
const first = items[0];
if (first !== undefined) {
console.log(first.toUpperCase());
}The same rule applies to index signatures such as Record<string, number>. It does not apply to tuples with a known length, so [string, number] still gives string for index 0. Inside a loop like for (let i = 0; i < items.length; i++), TypeScript still reports items[i] as possibly undefined. Use for...of, which gives you the element directly, or check for undefined. Avoid the non-null assertion ! as a habit, because it removes the protection you turned the flag on to get.
noImplicitOverride
When a subclass redefines a method from its base class, noImplicitOverride requires the override keyword. This catches two mistakes. One is renaming a base method and leaving the subclass method silently unrelated. The other is accidentally shadowing a base method with a new one.
class Animal {
speak(): string {
return "...";
}
}
class Dog extends Animal {
override speak(): string {
return "woof";
}
}If someone renames Animal.speak to makeSound, the override on Dog.speak becomes an error, because there is nothing left to override. Without the keyword, the code would compile and Dog would quietly stop overriding anything.
exactOptionalPropertyTypes
Normally timeout?: number accepts both a missing property and an explicit undefined. With exactOptionalPropertyTypes, an optional property may be absent, but it may hold undefined only if you write undefined in its type.
interface Options {
timeout?: number;
}
interface LooseOptions {
timeout?: number | undefined;
}
const a: Options = {};
const b: LooseOptions = { timeout: undefined };
// const c: Options = { timeout: undefined }; // error with the flag onThis matters when code distinguishes "not provided" from "provided as undefined", for example "timeout" in options or Object.keys. It can also create friction with libraries whose types were not written with this flag in mind. Of the flags in this chapter, it is the one most likely to cause errors at the boundary with third-party types.
Control-flow checks
Two flags catch bugs in function and switch logic.
noImplicitReturns reports functions where some paths return a value and others reach the end without returning.
function findName(id: number) {
if (id > 0) {
return "found";
}
// error with the flag on: Not all code paths return a value
}The fix is to add return undefined; at the end. This makes the choice visible to the reader.
noFallthroughCasesInSwitch reports a non-empty case that does not end in break, return, or throw.
function label(code: number): string {
let result = "";
switch (code) {
case 1:
result = "one";
// error with the flag on: fallthrough case in switch
case 2:
result = "two";
break;
}
return result;
}Empty cases that stack, such as case 1: case 2:, are still allowed.
noPropertyAccessFromIndexSignature
If a type has an index signature, this flag requires bracket access for keys that come from it. Dot access remains legal for properties you declared explicitly.
interface Env {
NODE_ENV: string;
[key: string]: string | undefined;
}
declare const env: Env;
const mode = env.NODE_ENV;
const home = env["HOME"];
// const bad = env.HOME; // error with the flag onThe bracket form tells the reader that HOME is a dynamic key and may not exist. It pairs well with noUncheckedIndexedAccess.
Unused code checks
noUnusedLocals reports local variables and imports that are never read. noUnusedParameters does the same for function parameters. A parameter whose name starts with an underscore is exempt, which is useful for callbacks that must accept an argument you do not use.
const values = [10, 20, 30];
const indexes = values.map((_value, index) => index);
console.log(indexes);Many teams leave these two flags to a linter such as ESLint, which can report them as warnings during development. The compiler reports them as errors, which can block a quick experiment or a build while you are debugging. If you enable them in tsconfig.json, expect to remove unused imports often.
Putting the checks into a shared config
Because the extra checks are separate flags, a shared base file lists them next to strict. This is the file from the earlier chapter plan, saved as tsconfig.base.json.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"skipLibCheck": true
}
}You can add the other flags from this chapter in the same place:
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true
}
}Common errors and gotchas
- Expecting
strictto include these flags. It does not, so a project with"strict": truecan still have unchecked array access. - Silencing
noUncheckedIndexedAccesswith!everywhere. This restores the old behavior and hides real empty-array bugs. - Enabling
exactOptionalPropertyTypesand then finding that a library's types reject your objects. Write| undefinedin your own types, or build the object without the property. - Turning on
noUnusedLocalsduring a refactor and getting hundreds of errors. Enable it after the refactor, or let your linter handle it. - Overriding an inherited method in a class with
noImplicitOverrideon and forgetting the keyword. The error message names the base class, so addingoverridefixes it.
Exercise
A project has "strict": true. Given const names: string[] = []; and const first = names[0];, what is the type of first, and which flag changes it to string | undefined? What must you write before calling first.toUpperCase() once the flag is on?
Show answer
Under plain strict, first has type string, so the compiler accepts first.toUpperCase() even though the array is empty and the call would fail at runtime. Enabling noUncheckedIndexedAccess changes the type to string | undefined. You must then narrow it first, for example with if (first !== undefined) { first.toUpperCase(); }.
Exercise
A class Dog extends Animal defines speak(). Someone renames Animal.speak to makeSound. Which flag and keyword make the compiler report the stale method in Dog?
Show answer
The noImplicitOverride flag, together with the override keyword on Dog.speak. Once the base method no longer exists, override produces an error because there is nothing to override.