TypeScript · Lesson 109 of 120
Project: Strictness
Turning it all on.
Strict mode is the start. A few extra settings catch even more bugs. The most useful one is no unchecked indexed access.

- strict is the start
- Extra settings
- noUncheckedIndexedAccess
Indexing can miss
With this setting, reading from an array or record by index might give undefined. So the first order might not exist. Run it. TypeScript makes us check, before reading its customer.
app.tstypescript
const orders = [{ id: 1042, customer: "Sam" }];
const stock: Record<string, number> = { LIGHTS: 0 };
const first = orders[0];
console.log(first.customer);
const helmets: number = stock["HELMET-M"];Output
app.ts line 5: 'first' is possibly 'undefined'. app.ts line 7: Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.
Also uses tsconfig.json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext",
"outDir": "out", "skipLibCheck": true, "types": ["node"]
},
"include": ["*.ts"]
}Extra strict settings
No unchecked indexed access treats lookups as possibly missing. No implicit returns requires every path to return. Exact optional property types separates missing from undefined. And no fallthrough cases in switch stops accidental fallthrough.
| Setting | Catches |
|---|---|
| noUncheckedIndexedAccess | missing items and keys |
| noImplicitReturns | forgotten returns |
| exactOptionalPropertyTypes | undefined vs missing |
| noFallthroughCasesInSwitch | switch fallthrough |
Summary
Extra strict settings catch even more bugs. Turn them on early, while the project is small. Next, the final run.
Key takeaways- noUncheckedIndexedAccess
- noImplicitReturns
- Turn on early
Next up: Project: Final Run — end to end