Strict mode and the compiler flags that matter

Intermediate · 21 min

What the strict flag turns on, the extra checks it leaves off like noUncheckedIndexedAccess, and which ones are worth enabling in a new project.

Before thistsconfig explained

Web Dev Simplified · 21 min

Strict mode and the compiler flags that matter

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Without `strict`, TypeScript is deliberately lenient so JavaScript can be adopted easily, which means it silently lets through the two most common bugs: values that are secretly `null` or `undefined`, and parameters that quietly become `any`. Since TypeScript 6.0, `strict` is on by default, so that leniency is now something a project opts into.
  • `strict: true` is an umbrella that enables a family of flags, including `strictNullChecks` and `noImplicitAny`, and new TypeScript releases can add members to that family, so upgrading the compiler can surface new errors.
  • Every new project should keep `strict` on, and older configs that set `"strict": false` are worth migrating. Turning it on later in a large codebase is far more work than living with it from day one.
  • `noUncheckedIndexedAccess` sits outside `strict` and makes array and dictionary lookups return `T | undefined`, catching out-of-bounds and missing-key bugs that `strict` alone misses.
  • `exactOptionalPropertyTypes`, `noImplicitOverride`, and `noFallthroughCasesInSwitch` are further opt-in checks. Each closes a specific gap, and each is worth evaluating rather than enabling by reflex.

Lenient on request, and why that’s costly

For most of its history, TypeScript’s defaults were lenient, so an existing JavaScript project could switch on the compiler and see a manageable number of errors. TypeScript 6.0 flipped that: with no options set, strict is now on. You still meet the lenient mode constantly, though, in older projects and in migrations that set "strict": false on purpose. That leniency has a cost. With strict off, this compiles cleanly:

function shout(message) {
  return message.toUpperCase();
}

const user = users.find((u) => u.id === 7);
console.log(user.name);

Two real bugs slip through. message has no annotation, so it silently becomes any, and any turns the checker off for everything it touches. find can return undefined when nothing matches, but without null checking the compiler pretends it can’t. Both lines crash at runtime under the right input, and the checker you installed to catch exactly this said nothing.

What strict turns on

"strict": true is a single switch that enables a family of flags. The two that matter most:

  • noImplicitAny — a parameter or variable whose type can’t be inferred is an error instead of a silent any.
  • strictNullChecks — null and undefined become separate types. users.find(...) returns User | undefined, and user.name is an error until you handle the missing case.

The rest of the family closes smaller holes:

Flag What it catches
strictFunctionTypes Callbacks whose parameter types are incompatible
strictBindCallApply Wrong arguments passed through .call, .apply, .bind
strictPropertyInitialization Class fields declared but never assigned in the constructor
noImplicitThis this used where its type is unknown
useUnknownInCatchVariables Treats catch (err) as unknown instead of any
alwaysStrict Emits "use strict" and parses in strict mode

Newer releases have added further members, such as strictBuiltinIteratorReturn. That’s the catch with an umbrella flag: when the family grows, every project with strict: true gets the new check on its next compiler upgrade. You can switch one member off explicitly, "strict": true, "strictPropertyInitialization": false, while you fix what it found.

Keep strict on in every new project; on TypeScript 6.0 and later that’s the default, so the main job is not to turn it off. Retrofitting it onto a 200-file codebase means hundreds of errors at once. Living with it from the first file costs almost nothing. The null-checking rules themselves get a full lesson in null, undefined, and strict null checks.

The checks strict leaves out

Some useful checks are too disruptive to put in the default family. These are the ones worth knowing.

noUncheckedIndexedAccess

Even under strict, indexing is trusted:

const names: string[] = [];
const first = names[0];      // string, according to the checker
first.toUpperCase();         // crashes: first is undefined

const prices: Record<string, number> = { apple: 1 };
prices["pear"].toFixed(2);   // crashes too

With noUncheckedIndexedAccess, names[0] and prices["pear"] are typed T | undefined, so you must check before using them. This catches a large class of real bugs. The cost is extra checks in loops over known-valid indexes, which is why it isn’t part of strict. For new code, it’s usually worth turning on.

exactOptionalPropertyTypes

Normally nickname?: string accepts three states: missing, a string, or an explicit undefined. This flag removes the third:

type Profile = { nickname?: string };

const p: Profile = { nickname: undefined };
// Error with exactOptionalPropertyTypes

That matters when code treats “key absent” and “key set to undefined” differently, such as "nickname" in p, or spreading { ...defaults, ...p }, where an explicit undefined overwrites a default. It’s precise, but it creates friction with libraries that weren’t written for it, so enable it deliberately.

Smaller ones

  • noImplicitOverride — a subclass method that replaces a parent method must say override, so renaming the parent method doesn’t silently leave an orphan.
  • noFallthroughCasesInSwitch — a non-empty case that falls into the next one is an error.
  • noImplicitReturns — every code path in a function must return explicitly if any path does.

A reasonable starting point for a new project:

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "noFallthroughCasesInSwitch": true
  }
}

Lint-style checks like noUnusedLocals are better left to a linter, which can report them as warnings rather than failing a build.

What the video covers

Kyle’s video is recent and organized the way you’d actually decide: must-have settings, optional extra checks, and personal-preference flags. It names noUncheckedIndexedAccess and the other non-strict extras with examples. It also tours some general tsconfig options, which overlap with the previous lesson, so treat those parts as review. For the strict family flag by flag, Harry Wolff’s older video in the links is still accurate on the core members, though it predates newer additions such as useUnknownInCatchVariables.

Check yourself

  1. 01With `strict` off, why does `function len(s) { return s.length; }` compile without complaint?

    Because `noImplicitAny` is off, so the unannotated parameter `s` silently gets type `any`, and the checker allows any operation on `any`. With `strict` on, `noImplicitAny` reports the missing annotation.

  2. 02Your project already has `strict: true`. Why can upgrading TypeScript still produce new errors?

    `strict` is a family whose membership grows. When a release adds a new strict-family flag, projects with `strict: true` get it automatically, along with whatever bugs it finds. You can switch an individual flag off explicitly while you fix them.

  3. 03With `noUncheckedIndexedAccess` on, what is the type of `names[0]` when `names` is a `string[]`, and why?

    It is `string | undefined`. The array might be empty or the index out of range, and the flag makes the checker admit that instead of assuming the element exists, so you must handle the missing case.

  4. 04What does `exactOptionalPropertyTypes` change about `{ nickname?: string }`?

    Without it, `nickname?: string` also accepts an explicit `nickname: undefined`. With it, the property may be missing, but if present it must be a `string`. That distinguishes "key absent" from "key set to undefined", which matters for code that checks with `in` or spreads objects.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark