Course contentsShow
← TypeScript
Lesson 1166 of 1,79627. Declaration FilesFree lesson

Adding New Exports to a Module

Declare additional functions, classes, or types as exports of an existing module you're augmenting.

Adding New Exports to a Module

What you'll learn: How to extend an existing module by declaring brand-new exports (functions, classes, types) that weren't originally part of it.

The Core Idea

When you augment a module (using declare module), you're not limited to just modifying existing interfaces or types. You can also add entirely new exports to a module that didn't have them before.

Think of it like adding new rooms to an existing building. You're not just remodeling what's already there—you're declaring that the building now has additional features that become part of its public interface.

Why Add New Exports?

Sometimes a third-party library is missing utilities or helpers you wish it had. Instead of creating separate wrapper files, you can augment the module directly to include your additions. This makes them feel native to the library and provides better autocomplete.

Basic Syntax

// Augmenting an existing module to add new exports
declare module 'some-library' {
  export function newHelper(): string;
  export class NewUtility {}
  export type NewType = { id: number };
}

After this declaration, TypeScript believes some-library exports newHelper, NewUtility, and NewType alongside its original exports.

Practical Example: Extending a Library

// Original library has minimal exports
declare module 'config-lib' {
  export const version: string;
}

// You augment it with helper functions
declare module 'config-lib' {
  export function validateConfig(cfg: unknown): boolean;
  export class ConfigError extends Error {}
}

// Now you can import both original and new exports
import { version, validateConfig, ConfigError } from 'config-lib';

TypeScript now treats validateConfig and ConfigError as legitimate exports from config-lib, giving you full type-checking and autocomplete.

When to Use This

This pattern shines when you're extending third-party modules with project-specific utilities or polyfilling missing features during migration. Just remember: you're only adding type information—you still need to provide the actual runtime implementation separately.

Key Takeaway: Module augmentation lets you declare brand-new exports on existing modules, extending their type signature to include functions, classes, or types that weren't originally there.