.d.ts basics; export type vs export
Basics of declaration files: ambient vs module declarations, export type vs export, and ensuring correct DX for consumers.
.d.ts basics; export type vs export is a free TypeScript Academy lesson on CoddyKit. This is lesson 1 of 2. 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 2 lessons in total.
Intro
Goal: Write useful .d.ts files. You will learn ambient vs module declarations, how to declare exports, and when to use export type vs export.
- Ambient vs module context
- Type-only vs value exports
- Good practices
Ambient declarations
Use declare global to extend the global scope. Add export {} to mark the file as a module and avoid conflicts.
// globals.d.ts
// Ambient declaration: no import/export, augments global scope
declare global {
interface Window {
APP_VERSION: string
}
}
export {} // ensures this file is a moduleModule declarations
declare module describes the shape of an external package. Use exports inside to mirror what the JS provides.
// types/index.d.ts
// Declare a module with exports
declare module "mylib" {
export function greet(name: string): string
export const version: string
}Export type vs export
export type (or interface) is for type-only constructs. Use export for functions, constants, classes—things that exist at runtime.
// shapes.d.ts
export type Point = { x: number; y: number }
export interface User {
id: string
name: string
}
export function distance(a: Point, b: Point): number
export const VERSION: stringAugmenting modules
You can augment existing modules by reopening them in another declaration file. Useful for patching third-party packages.
// augment.d.ts
import "mylib"
declare module "mylib" {
export function extra(): void
}Best practices
Best practices:
- Keep .d.ts minimal; avoid unnecessary globals.
- Separate type-only from value exports clearly.
- Test declaration files with
tsc --noEmitand sample usage.
export type check
Quick check: When should you use export type?
Recap
Recap: .d.ts files declare types without implementation. Use declare global or declare module, separate export type vs export, and follow best practices.
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 “.d.ts basics; export type vs export” lesson free?
Yes — the full text of “.d.ts basics; export type vs export” 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 2 lessons in total.
What will I learn in “.d.ts basics; export type vs export”?
Basics of declaration files: ambient vs module declarations, export type vs export, and ensuring correct DX for consumers. 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 1 of 2.
How long does the “.d.ts basics; export type vs export” 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
- .d.ts basics; export type vs export
- Publishing packages with types