JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstypescript
Prev

Learn the concept

Declaration Files

typescript
senior
declarations

How do you write declaration files for JavaScript libraries?

declaration-files
d.ts
ambient
augmentation
types
Quick Answer

Declaration files (.d.ts) provide type information for JavaScript code without implementation. They use 'declare' keyword for ambient declarations, support module augmentation for extending existing types, and global augmentation for adding to global scope.

Detailed Explanation

Declaration File Basics:

  • .d.ts extension - types only, no runtime code
  • declare keyword for ambient declarations
  • No function bodies or initializers
  • Describes shape of JavaScript values

Types of Declarations:

  • Ambient modules: Declare external module types
  • Global: Declare global variables/types
  • Module augmentation: Extend existing modules
  • Global augmentation: Add to global scope

Best Practices:

  • Match runtime API exactly
  • Use JSDoc for additional documentation
  • Test with actual usage
  • Consider contributing to DefinitelyTyped

Code Examples

Basic declaration fileTypeScript
// types/my-lib.d.ts

// Declare a function
declare function greet(name: string): string;

// Declare a variable
declare const VERSION: string;

// Declare a class
declare class MyClass {
  constructor(options: MyClassOptions);
  start(): void;
  stop(): void;
  readonly status: 'running' | 'stopped';
}

interface MyClassOptions {
  name: string;
  timeout?: number;
}

// Declare a namespace (for libraries with nested structure)
declare namespace MyLib {
  function init(config: Config): void;
  
  interface Config {
    apiKey: string;
    debug?: boolean;
  }
  
  namespace Utils {
    function format(value: unknown): string;
  }
}

// Export for module usage
export { greet, VERSION, MyClass, MyLib };

Real-World Applications

Use Cases

Legacy Library Typing

Writing declaration files for untyped JavaScript libraries used in enterprise codebases

API SDK Type Authoring

Creating precise .d.ts files for SDK distribution with conditional exports for ESM/CJS

Global Type Extension

Using ambient declarations and module augmentation to add custom properties to third-party types

Mini Projects

Type a JavaScript Library

intermediate

Write comprehensive declaration files for a small untyped JavaScript utility library

Package.json Exports Types

advanced

Create a package with dual CJS/ESM support, separate .d.ts/.d.mts files, and correct package.json exports

Industry Examples

DefinitelyTyped

Community hub for high-quality TypeScript declarations for JavaScript packages

Deno

Uses .d.ts files for its built-in APIs and supports JSDoc-based type generation

Resources

TypeScript - Declaration Files

docs

DefinitelyTyped

docs

Related Questions

How do TypeScript modules work and what's the difference between import types?

mid
modules

What are generics in TypeScript and how do you use them?

mid
generics
Previous
How do you handle complex generic constraints and variance in TypeScript?
Prev