Lab 14: tsconfig Advanced

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

target/lib/module/moduleResolution, strict flags, project references, composite builds, incremental, paths/baseUrl, noUncheckedIndexedAccess, exactOptionalPropertyTypes.


Step 1: Setup

docker run -it --rm node:20-alpine sh
npm install -g typescript ts-node
mkdir /lab14 && cd /lab14

Step 2: target, lib, module

{
  "compilerOptions": {
    // target: which JS version to emit
    // ES5 = IE11 compat, ES2020 = modern Node/browser
    "target": "ES2020",

    // lib: which built-in APIs TypeScript knows about
    // Defaults to match target, but you can override
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    // For Node.js only (no DOM):
    // "lib": ["ES2020"],

    // module: module format for output
    // "commonjs"   = require/module.exports (Node.js, ts-node)
    // "ESNext"     = import/export (bundlers, browser)
    // "NodeNext"   = Node.js ESM with .js extensions
    "module": "commonjs",

    // moduleResolution: how to resolve imports
    // "node"    = classic Node.js algorithm (recommended for ts-node)
    // "bundler" = bundler-friendly (TS 5.0+)
    // "NodeNext"= matches Node.js ESM
    "moduleResolution": "node"
  }
}

Step 3: Strict Flags Explained


Step 4: noUncheckedIndexedAccess


Step 5: exactOptionalPropertyTypes


Step 6: Project References

💡 composite: true enables project references. TypeScript builds only what changed. Use tsc --build (or tsc -b) to build the whole project graph.


Step 7: Incremental Builds


Step 8: Capstone — Show Config

📸 Verified Output:


Summary

Option
Description
Recommended

target

JS version to emit

ES2020 for Node 14+

module

Module format

commonjs (ts-node), ESNext (bundlers)

moduleResolution

Import resolution

node (ts-node), bundler (Vite)

strict

Enable all strict flags

✅ Always

noUncheckedIndexedAccess

arr[0] = T | undefined

✅ Recommended

exactOptionalPropertyTypes

?:undefined

Consider

composite

Enable project references

For monorepos

incremental

Cache build state

For large projects

skipLibCheck

Skip node_modules type check

✅ Usually

isolatedModules

Each file is a module

For esbuild/swc

Last updated