Ambient declarations (.d.ts) & third-party typings
Write ambient declarations in .d.ts files to describe globals and modules; install and use third‑party typings safely.
Ambient declarations (.d.ts) & third-party typings is a free TypeScript Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.
Intro
Goal: Describe external JavaScript with ambient declarations in .d.ts files and use third‑party typings for libraries that lack types.
Global declarations
globals.d.ts: Declare globals and extend ambient interfaces (e.g., Window) without emitting JS.
// file: globals.d.ts
// Declare global variables and interfaces
declare const VERSION: string;
declare interface Window {
appReady?: boolean;
}Lambda with receiver basics
- Receiver = the builder instance used inside the lambda.
- Cleaner calls: item("Tea") instead of builder.item.
- Keep names short and actions obvious.
Module declarations
declare module describes a package that ships no types. Now import { coolify } type-checks correctly.
// file: cool-lib.d.ts
// Provide types for an untyped JS module
declare module "cool-lib" {
export function coolify(s: string): string;
export const version: string;
}Module augmentation
Module augmentation extends installed typings, e.g., add a custom prop to an existing interface.
// file: react-augment.d.ts
// Extend an existing module (augmentation)
declare module "react" {
interface HTMLAttributes<T> {
dataTestId?: string;
}
}Third‑party typings
Prefer @types/ packages from DefinitelyTyped when a library ships no types. They add only .d.ts files to your build.
// install: npm i -D @types/lodash
// usage:
import _ from "lodash";
const r = _.chunk([1,2,3,4], 2);Tips & resolution
Tips:
- Place your .d.ts under src/ or include them via tsconfig.json (
include/typeRoots/types). - Use module augmentation to extend, avoid editing node_modules.
- Avoid overly-broad
any; specify minimal accurate shapes.
Ambient declarations check
Quick check: What is an ambient declaration used for?
Recap
Recap: Use .d.ts files for globals/modules, prefer @types for community typings, and extend safely with module augmentation.
Recap
Recap: Use typed receivers per block, add @DslMarker for scope safety, and return an immutable model for clean usage.
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “Ambient declarations (.d.ts) & third-party typings” lesson free?
Yes — the full text of “Ambient declarations (.d.ts) & third-party typings” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 3 lessons in total.
What will I learn in “Ambient declarations (.d.ts) & third-party typings”?
Write ambient declarations in .d.ts files to describe globals and modules; install and use third‑party typings safely. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
How long does the “Ambient declarations (.d.ts) & third-party typings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ES Modules (named vs default), re-exports
- Ambient declarations (.d.ts) & third-party typings
- Path mapping & module resolution