# tsconfig Compiler Flags

> Dialing the compiler's strictness.

Subject: TypeScript | Lesson 47 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/tsconfig-compiler-flags
Previous: [Modules vs Namespaces](https://codewithanimation.in/typescript/modules-vs-namespaces.md) | Next: [Module Resolution](https://codewithanimation.in/typescript/module-resolution.md)

## The Rulebook

The `tsconfig.json` file is the heart of a TypeScript project. It tells the compiler how strict to be and how to emit JavaScript.

The most important flag is `"strict": true`, which enables a suite of type-checking rules that make TypeScript actually useful.

## The Billion Dollar Mistake

Without strict mode, TypeScript allows variables to be `null` or `undefined` implicitly. This means you might try to call `.toUpperCase()` on an undefined string, causing a runtime crash.

By enabling `strictNullChecks` (which is included in `strict`), the compiler forces you to handle the `null` case before accessing properties.

## Syntax

```json
// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "strict": true,
    "exactOptionalPropertyTypes": true,
    "noImplicitAny": true
  }
}
```

## Common mistake: Migrating Legacy Code

Turning on `strict` in an old JavaScript codebase that was just migrated to TypeScript will result in thousands of errors.

Teams often have to leave `strict: false` on initially, and manually enable flags one by one (like `noImplicitAny`) as they clean up the code.

## Tip: exactOptionalPropertyTypes

This is a powerful modern flag! Without it, if an interface has `name?: string`, you can explicitly set `name = undefined`. With it, you must omit the key entirely, preventing dirty `{ name: undefined }` objects in your database.

## Try it: Try it

Toggle the `strictNullChecks` flag in the virtual `tsconfig.json` and watch how the compiler instantly reacts to unsafe code.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/tsconfig-compiler-flags

## Check yourself

### Question 1

What happens if strictNullChecks is FALSE?

- A. TypeScript throws an error if you try to assign null to anything.
- B. TypeScript treats null and undefined as valid values for every type (like string or number), potentially causing runtime crashes.
- C. TypeScript removes null values from arrays automatically.

Correct answer: B

Without strictNullChecks, TypeScript essentially ignores null and undefined safety. You could assign null to a number, and the compiler would allow it, leading to a catastrophic runtime error.

## Remember this

- Always turn on `"strict": true` for new projects.
- `strictNullChecks` forces you to handle `null`/`undefined` cases.
- `noImplicitAny` forces you to explicitly type arguments.
- `tsconfig.json` defines the rules of engagement between you and the compiler.
