Working in a Real Project: modules and tsconfig

Harry · 14 Sep 2026 · 2 views
Advertisement
Advertisement

Modules: import and export

Each file is a module. Export what other files may use, and import it by path:

// math.ts
export function add(a: number, b: number) { return a + b; }
export const PI = 3.14159;

// app.ts
import { add, PI } from "./math";
console.log(add(2, 3), PI);

A default export is imported without braces; named exports use braces. Prefer named exports for clarity in larger codebases.

tsconfig.json

This file controls the compiler. The settings you touch most often:

{
  "compilerOptions": {
    "target": "ES2020",       // JS version to emit
    "module": "ESNext",       // module system
    "outDir": "dist",         // where compiled JS goes
    "rootDir": "src",         // where the source lives
    "strict": true            // all strict checks on
  }
}

Turn on strict mode

"strict": true is the single most valuable setting. It enables a family of checks – notably strict null checks, which force you to handle null and undefined explicitly, just like Kotlin's null-safety. Start new projects with it on.

let name: string | undefined = maybeName();
name.toUpperCase();          // Error under strict: might be undefined
name?.toUpperCase();         // OK: optional chaining

Building and running

tsc            # compile src -> dist
node dist/app.js

Tools like ts-node (run TS directly) and bundlers (Vite, esbuild, webpack) streamline development, but under the hood they all use the TypeScript compiler.

Key points

  • Use export/import to split code into modules.
  • tsconfig.json sets the target, module system, output folder and strictness.
  • Enable "strict": true – especially strict null checks – on every project.
  • ?. optional chaining safely accesses possibly-undefined values.
Share this post:

Comments (0)

Please login or register to comment.