Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Type Inference  ›  Lesson

tsconfig.json

tsconfig.json 12 mintsconfig.json marks the root of a TypeScript project and says which files to compile and with which compiler options.

tsconfig.json

Run npx tsc in a folder, and how does the compiler know which files to check, how strict to be, or where to write the JavaScript? It reads one file: tsconfig.json. Everything about how your project compiles lives there.

The rule

tsconfig.json marks the project: which files, which options, which output.

The folder containing tsconfig.json is the root of a TypeScript project. When you run tsc without file names, it looks for this file in the current folder and then in parent folders.

A small configuration for the task tracker:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true
  },
  "include": ["src"],
  "exclude": ["src/**/*.test.ts"]
}

The top-level keys answer "which files?":

  • include — folders or patterns to compile. "src" means every TypeScript file under src/.
  • exclude — patterns to leave out of what include matched. (node_modules is excluded by default.)
  • files — an exact list of files, used instead of or alongside include in small projects.
  • extends — inherit settings from another config file, such as a shared base for several packages.

compilerOptions answers "how?":

  • rootDir and outDir — where the sources live and where the .js files go. src/tasks/list.ts becomes dist/tasks/list.js, keeping the folder structure.
  • target and module — what kind of JavaScript to produce (the next lesson covers these).
  • strict — how hard to check.

The file allows comments, unlike ordinary JSON, which is why tsc --init fills it with explanatory notes.

The shortcut

Read any tsconfig in two passes: "which files?" then "what options?"

Predicting where a file ends upStep 1 of 4
  1. Is it included?

    src/tasks/list.ts is under src, which include lists. Yes.

A frequent confusion: exclude only filters what include matched. If an included file imports an excluded one, the compiler still reads the imported file.

Drills

Answer fast — trust the rule.

quick drill

Which key holds settings such as strict and outDir?

quick drill

With rootDir: "src" and outDir: "dist", where is src/api/tasks.ts written?

quick drill

What does "extends": "./tsconfig.base.json" do?

quick drill

How does running npx tsc with no arguments find its settings?

Remember

  • tsconfig.json marks the project: which files, which options, which output.
  • Read it in two passes: include/exclude/files first, then compilerOptions.
  • Trap: exclude does not stop a file from being compiled if an included file imports it.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead a tsconfig

Write a small tsconfig.json for a project whose source is in src/ and whose output should go to dist/, with strict checking on. Then explain what each field you used does.