Sikau

TypeScript · Day 23 · about 25 min

Modules and declaration files

How TypeScript fits into real projects.

Beyond one file

Real projects have many files, use npm packages, and run a build step. Here's how TypeScript fits in: modules share types, declaration files describe JavaScript libraries, and tsconfig.json sets the rules.

Sharing types between files

// types.ts
export interface Lesson {
  id: string;
  title: string;
}

// lessons.ts
import type { Lesson } from "./types";
export const lessons: Lesson[] = [{ id: "t01", title: "Why TypeScript?" }];
  • Declaration files (.d.ts) hold only types. They describe JavaScript code — like the browser itself (lib.dom.d.ts, which powered the last lesson) or npm packages.
  • Many packages include their types. For others, install @types/<name> (from the DefinitelyTyped project).
  • tsconfig.json configures the compiler. Turn on "strict": true — it's what you've used throughout this course.
  • Build tools (Vite, Next.js, esbuild) remove types quickly; tsc --noEmit does the full type check, often in CI.

The rest of this lesson

  • Sharing types across filesexplain
  • tsconfig.json essentialsexplain
  • What's in the JavaScript output?predict
  • Project piecesmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 24 lessons of TypeScript