0Pricing
TypeScript Academy · Lesson

.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 — 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, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 module

Module 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: string

Augmenting 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 --noEmit and 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.

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, and the TypeScript Academy course includes 2 lessons in total. 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.

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; this is — lesson 1 of 2, so you can start here or from the beginning and move at your own pace.

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

  1. .d.ts basics; export type vs export
  2. Publishing packages with types
← Back to TypeScript Academy