TypeScript Foundations·Types that pay for themselves

tsconfig for beginners

Read the most common compiler flags and understand what each one actually changes.

Intermediate 10 min read Lesson 5 of 300 Challenge included
What you will be able to do
  • Turn on strict to enable a broad set of safety checks.
  • Set target to the JavaScript version your runtime supports.
  • Set module to how your bundler imports and exports code.
  • Turn on noUncheckedIndexedAccess to catch missing lookups.

In plain English

The tsconfig file tells the TypeScript compiler how to behave. The strict flag turns on a bundle of checks that catch more mistakes, including accidental null and any. The target flag decides which JavaScript version the compiler emits, so newer targets keep newer syntax and older targets rewrite it. The module flag decides how files import and export each other. The noUncheckedIndexedAccess flag makes every array or record lookup possibly undefined, so you must handle a missing value before using it.

Step by step

  1. 1Open or create a tsconfig.json at the root of your project.
  2. 2Set strict to true and fix the errors it reports.
  3. 3Set target to the JavaScript version your environment supports.
  4. 4Set module to match your bundler, often ESNext.
  5. 5Turn on noUncheckedIndexedAccess and handle undefined lookups.
tsconfig.jsonJSON
{
  "compilerOptions": {
    "strict": true,
    "target": "ES2022",
    "module": "ESNext",
    "noUncheckedIndexedAccess": true,
    "noEmit": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

Common mistakes

  • Turning off strict to clear errors hides real bugs instead of fixing them.
  • Setting target to a version newer than your runtime means your users get syntax their browser cannot run.
  • Forgetting noUncheckedIndexedAccess lets a missing array item crash at runtime instead of at compile time.
  • Changing module without updating your imports can break how files load each other.
Your turn

Make the config strict

Easy

Add strict and noUncheckedIndexedAccess to a tsconfig. Explain in your own words why reading items[0] can now fail.

JSON · starter
{
  "compilerOptions": {}
}
Check your understanding before moving on.
Knowledge checkQuestion 1

What does noUncheckedIndexedAccess change?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Types and inference
Lesson 5 of 300 · 0% of course