← TypeScript

Basic · 13 of 13 · 14 min read

tsconfig and strict

The handful of compiler options that actually change how you write code, what strict turns on, and why tsc --noEmit is the command most projects run.

  • tsconfig

tsconfig.json tells the compiler which files to look at, what to believe exists at runtime, and how strict to be. Most of its hundred options never matter; about eight change how you write code every day.

A working starting point

{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "lib": ["es2022", "dom"],
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "verbatimModuleSyntax": true,
    "isolatedModules": true,
    "skipLibCheck": true,
    "noEmit": true
  },
  "include": ["src"]
}

Nothing exotic, and every line earns its place below.

strict is a family

It is one switch for about eight flags. The ones you will notice:

Flag What it stops
strictNullChecks null and undefined being assignable to every type
noImplicitAny a parameter with nothing to infer from silently becoming any
strictFunctionTypes unsound parameter assignability on function types
strictPropertyInitialization a class field that is never assigned
noImplicitThis this being any inside a plain function
useUnknownInCatchVariables a caught value being typed any
// strictNullChecks — by far the biggest one
let name: string = "Ana";
// name = null; // 'null' is not assignable to 'string'

function find(id: number): string | undefined {
  return id === 1 ? "Ana" : undefined;
}
console.log(find(2)?.toUpperCase() ?? "not found");

// useUnknownInCatchVariables
try {
  throw new Error("boom");
} catch (error) {
  // console.log(error.message); // 'error' is of type 'unknown'
  if (error instanceof Error) console.log(error.message);
}

Nothing to do with JavaScript’s "use strict" directive, which is a runtime thing. The names collide and the concepts do not touch.

You can turn the umbrella on and one member back off, which is the usual way to adopt it gradually on an existing codebase:

{
  "compilerOptions": {
    "strict": true,
    "strictPropertyInitialization": false
  }
}

The one strict leaves out

noUncheckedIndexedAccess is not in the family, and it closes a real hole:

// without the flag
const items: string[] = [];
const first = items[0];           // typed string
console.log(first.toUpperCase()); // compiles, throws

// with the flag
// const first = items[0];        // string | undefined
// console.log(first?.toUpperCase() ?? "empty");

It is excluded because it is noisy in index-based loops. It is still usually worth it: the noise is exactly where off-by-one bugs live.

target and module are different questions

{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext"
  }
}

target decides the syntax level — whether an arrow function or a class survives or gets rewritten for an older engine. module decides the import/export format of the output.

// source
export const double = (n: number) => n * 2;

// target es5 + module commonjs:
//   exports.double = function (n) { return n * 2; };

// target es2022 + module esnext:
//   export const double = (n) => n * 2;

lib is a third, independent question: which built-in declarations exist. target sets a default for it, but you can emit ES2017 syntax while telling the compiler Promise.any exists because you ship a polyfill. It injects nothing — only the declarations change.

{
  "compilerOptions": {
    "target": "es2017",
    "lib": ["es2022", "dom"]
  }
}

tsc --noEmit, and why

Most projects do not compile with tsc. Vite, esbuild and SWC strip the types file by file, which is an order of magnitude faster — and which does not check them at all.

{
  "scripts": {
    "dev": "vite",
    "typecheck": "tsc --noEmit",
    "build": "npm run typecheck && vite build"
  }
}

So tsc --noEmit is frequently the only thing in the pipeline that checks the types, and leaving it out of CI means the type system quietly stops doing its job.

Two flags exist specifically to keep single-file transpilers honest:

  • isolatedModules rejects anything that cannot be transpiled one file at a time — a re-exported type without export type, a const enum from another module.
  • verbatimModuleSyntax requires import type on type-only imports, so whether an import survives into the output is something you wrote rather than something the compiler inferred.
import type { User } from "./user";   // always erased
import { createUser } from "./user";  // kept — it is a value
import "./polyfills";                 // kept, for the side effect
console.log(typeof createUser);

Files

{
  "include": ["src"],
  "exclude": ["node_modules", "dist"],
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "dist"
  }
}

include and exclude choose the files; rootDir and outDir decide where the emit lands. exclude only removes files from include — it does not stop a file being pulled in by an import.

Two more worth knowing by name:

  • skipLibCheck: true skips type checking inside .d.ts files. Almost every template sets it, because two dependencies shipping conflicting types is common and not your problem. The cost is that a genuine error in your own declaration file goes unreported.
  • paths maps an alias to a folder — for the compiler only. The emitted import is unchanged, so the bundler and the test runner each need the same alias configured separately. It is a common source of “it type-checks and crashes at runtime”.
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": { "@lib/*": ["src/lib/*"] }
  }
}

Adopting strict on an existing codebase

{
  "compilerOptions": {
    "strict": false,
    "noImplicitAny": true,
    "strictNullChecks": true
  }
}

Turning the members on one at a time is less disruptive than the umbrella, and strictNullChecks first usually produces the most errors and the most value. The alternative — // @ts-expect-error on the existing failures, then strict: true — has the advantage that the comment fails the build once the underlying problem is fixed, so the list cannot rot.

Common mistakes

Assuming the build checks the types

Mistake. “It builds, so the types are fine.” Why it fails. The bundler transpiles without checking. The build succeeds whatever the types say. What to do instead. tsc --noEmit as its own script, and in CI.

Confusing target with lib

Mistake. Setting target: "es5" and expecting Promise to disappear from the type system. Why it fails. target is about syntax; lib is about which APIs the compiler believes exist. Neither emits a polyfill. What to do instead. Set them separately, and ship the polyfill yourself.

Configuring paths and nothing else

Mistake. Adding @lib/* to the tsconfig and importing from it. Why it fails. paths is compiler-only. The emitted import still says @lib/thing, which nothing at runtime can resolve. What to do instead. Mirror the alias in the bundler and the test runner — or use the imports field in package.json, which both understand.

Check yourself

3 questions from the bank on what you just read. Answering here does not affect your quiz history.

What does the strict flag in tsconfig.json do?

What is the difference between target and module in the compiler options?

What does tsc --noEmit do?

Practise tsconfig →

Further reading

Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.

Reaching the end of a lecture marks it as read.