tsconfig.json Explained: Strict Settings That Catch Bugs

strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes and the other tsconfig options that catch real bugs, with a recommended setup.

In this chapter

tsconfig that catches bugs

More on The compiler

Keep learning

Overview

Object is possibly 'undefined'. (noUncheckedIndexedAccess) Parameter 'amount' implicitly has an 'any' type. (noImplicitAny) 'latest' is possibly 'undefined'. (strictNullChecks) Type 'undefined' is not assignable to type 'number'. (exactOptionalPropertyTypes) 'e' is of type 'unknown'. (useUnknownInCatchVariables) Property 'message' does not exist on type 'unknown'. (useUnknownInCatchVariables) Property 'total' has no initializer and is not definitely assigned in the constructor. This member must have an 'override' modifier because it overrides a member in the base class 'Base'. all off strict: true strict + extras Compiler flags Needs strictNullChecks (TS5052 otherwise) Part of strict Not part of strict (needs strictNullChecks) bugs caught before runtime bugs that ship

One file, seven bugs. All compile with every flag off.

chapters · the compiler tsconfig that catches bugs The same code can be safe or broken. The flags in tsconfig.json decide which. tsconfig.json is the settings file for tsc, the TypeScript compiler (the program that checks your code). A flag is one on/off setting inside it.

1. The problem: no errors in the build, crash for real users

Below, refund looks up an order by id and returns its total. Two bugs hide in it: the order may not exist, and the caller passes a number (42) where ids are strings. id: any No errors. id got the type any (“skip all checks”). order is shown as always found. At runtime nothing matches 42, so order is undefined and line 6 crashes: “Cannot read properties of undefined”. Parameter 'id' implicitly has an 'any' type. 'order' is possibly 'undefined'. Order | undefined Same code. Two real bugs found. Line 4: TypeScript could not work out a type for id, and strict mode forbids silently using any. Line 6: find() returns undefined when nothing matches, so reading .total may crash. Without strictNullChecks, find() is typed as if it always finds something. Order | undefined means “an Order, or nothing”.

2. strict is a family of flags

"strict": true is one switch that turns on a group of flags. New TypeScript versions can add more flags to the group. Each row below names one flag and the bug it stops. stops this bug Without the flag, a parameter with no type becomes any (no checks at all), with no warning. find() → Order | undefined null and undefined become their own types, so “maybe missing” shows up in the type. A callback must accept every event it may get. Example just below this table. Fields must be set in the constructor. catch (e) → e: unknown Anything can be thrown, not only Error. fn.call(this, "wrong") bind, call and apply check their arguments. "use strict" in output JavaScript strict mode in every file. TS 5.6+: an iterator hands out values one by one. When finished it gives undefined, so value is typed T | undefined. Run tsc --showConfig to see every flag that strict expanded to in your project. “Not assignable” means “does not fit”. A keyboard event could arrive, and it has no clientX. So onClick does not fit a Handler. method syntax handle(e): still loose No error, same bug. Methods stay bivariant (TypeScript accepts the parameter in either direction) even under strict.

How TypeScript rejects h = onClick

Handler promises callers they may pass MouseEvent | KeyboardEvent onClick only accepts Does every possible input fit onClick? Try KeyboardEvent KeyboardEvent → MouseEvent ✕ KeyboardEvent lacks clientX, button, … error: not assignable to 'Handler'

3. Lab: flip the flags

Start with everything off. Turn flags on one at a time and watch which line lights up. why it is a bug, in plain words 3 menu[item] the item may not be on the menu Then menu[item] is undefined and .toFixed crashes. 4 fee(amount) amount has no type TypeScript could not work out a type and gave up (any). 7 latest.id find() may find nothing latest can be undefined. 8 tip: undefined tip? means “may be left out” With exactOptionalPropertyTypes, writing undefined is not the same as leaving it out. 9 e.message anything can be thrown e is unknown (“could be anything”), so you must check it first. 10 total: number total is never set It starts as undefined, not a number. replaces Base.pay without saying so Write override pay() so a rename in Base is caught. Flags that need strictNullChecks strictPropertyInitialization, exactOptionalPropertyTypes. tsc refuses the config without it (error TS5052). noUncheckedIndexedAccess does nothing without it. Flags that stand alone noImplicitAny, useUnknownInCatchVariables, noImplicitOverride. Each works on its own, so you can adopt them one by one.

4. Worth turning on beyond strict

These flags are not part of strict. Pick a tab to see the bug each one catches. number | undefined item: string 'sushi' is possibly 'undefined'. Object is possibly 'undefined'. An index lookup (reading with [ ]) may miss: there is no sushi, and queue is empty. So the result gets | undefined (“or nothing”). A for…of loop only visits items that exist, so item stays string. Type 'undefined' is not assignable to type 'number'. tip? now means “may be left out”, not “may be set to undefined”. So undefined does not fit where a number is expected. This matters when code checks "tip" in obj (“does the key exist?”): for c that check says yes, yet there is no tip. This member must have an 'override' modifier because it overrides a member in the base class 'Notifier'. Write override send to say “I replace the parent’s send on purpose”. If someone later renames Notifier.send, the override line errors. Without it, SmsNotifier.send quietly stops replacing anything. Code that calls the new name runs the parent’s version instead. Property 'API_KEY' comes from an index signature, so it must be accessed with ['API_KEY']. An index signature is a type that allows any key, like Record<string, string>. This optional flag makes you write env["API_KEY"], so the brackets warn readers “this key may not exist”.

5. target, lib and module are three different questions

tsc turns your .ts files into .js files. Writing those output files is called emit. Three settings shape that output and the checking. Which built-in APIs TypeScript believes exist: Array.at, Promise.any, document. Only affects checking. How import/export is written: ESM, CommonJS, or left alone for a bundler. lib: ["es2017"]: the type checker does not know newer APIs Property 'at' does not exist on type 'number[]'. Try changing the 'lib' compiler option to 'es2022' or later. Property 'flat' does not exist on type 'number[][]'. Try changing 'lib' to 'es2019' or later. Cannot find name 'document'. Try changing the 'lib' compiler option to include 'dom'. What does tsc output for line 3? ids[ids.length - 1] a polyfill + ids.at(-1) a compile error Step 1: lib includes es2022, so the checker knows .at and there is no error. Step 2: target only rewrites syntax (operators and keywords). ?? is syntax, so line 4 becomes last !== null && last !== void 0 ? last : 0. Step 3: .at is a method call, not syntax, so it is copied as is. Old browsers crash unless you add a polyfill (extra code that adds the missing method) yourself. lib too new for your runtime lib matches the runtime

tsconfig.json (Vite / Next / any bundler that does the emitting)

what it means jsx: "react-jsx" understand <div> tags in .tsx files allow import data from "./x.json" moduleDetection: "force" treat every file as its own module Names in one file do not clash with another file. Bundlers translate each file alone, without types. So the bundler knows which imports to delete. do not re-check .d.ts files strict + 2 extras module: preserve, isolatedModules skipLibCheck: skip .d.ts in node_modules tsc only checks noEmit: the bundler writes JS module: "preserve" (TS 5.4+) implies moduleResolution: "bundler". For a library you publish, use "nodenext" and drop noEmit.

Remember

Defaults are loose: no strict means any and undefined slip through. Set lib to what your runtime really has, not what you wish it had. ! Cheat code strict: true on day one. Add noUncheckedIndexedAccess. Match lib to the runtime, because target never polyfills. → Go deeper: inside the compiler, any, unknown and never, modules and ESM, migrating JavaScript.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.