1. Concept library
  2. JavaScript & TypeScript
  3. Module 8: Modules and tooling

Concept

Declaration files

A .d.ts file describes the types of JavaScript code without its implementation. Libraries ship them or get them from @types, and you write one to describe a module the compiler does not know.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what a .d.ts file describes and where libraries get theirs
  2. Install types from @types and explain what happens when none exist
  3. Write a minimal declaration for a module the compiler does not know

In practice

A few lines that show Declaration files doing its job.

TypeScript
// types/legacy-widget.d.ts
declare module "legacy-widget" {
  export interface Options { size?: number }
  export function mount(el: HTMLElement, options?: Options): () => void;
}

// app.ts
import { mount } from "legacy-widget";
const unmount = mount(document.body, { size: 2 });

The common mistake

Watch for

Writing declare module "x" with no body to silence "could not find a declaration file". It makes every import from x an any, which is worse than a minimal typed declaration.

What to know first

Your roadmap puts these before Declaration files, unless you already know them.

How it stays learned

Once a session shows you understand Declaration files, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← tsconfig essentials