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

Declaring Classes

Use declare class to describe the shape of classes, including constructors, methods, and properties.

Declaring Classes in Declaration Files

You'll learn: How to use declare class to describe the shape of classes in declaration files, including constructors, methods, and properties.

What This Means

When you're writing a .d.ts file to provide types for JavaScript code (like a library), you need to describe what classes exist and what they look like. The declare class syntax lets you define the "shape" of a class—its constructor signature, properties, and methods—without providing the actual implementation.

Think of it like drawing a blueprint. You're not building the house (the actual class logic lives in JavaScript), but you're documenting exactly what rooms exist, where the doors are, and what features are available. TypeScript uses this blueprint to type-check your code.

The declare class Syntax

// In a .d.ts file
declare class Logger {
  // Properties
  level: string;
  readonly logCount: number;
  
  // Constructor
  constructor(name: string, level?: string);
  
  // Methods
  log(message: string): void;
  error(message: string): void;
  getStats(): { count: number; level: string };
}

This declaration tells TypeScript: "A class called Logger exists somewhere in the JavaScript runtime. Here's what it looks like." You can now use Logger in your TypeScript code with full type safety, even though the implementation is in plain JavaScript.

Key Features to Declare

You describe three main parts:

  1. Constructor signature: What parameters the class accepts when instantiated
  2. Properties: Instance variables with their types (including readonly if applicable)
  3. Methods: Function signatures that return typed values

You can also declare static members, access modifiers (public, private, protected), and optional properties—everything you'd use in a regular TypeScript class.

Key Takeaway: declare class creates a type-level description of a class that exists in JavaScript, enabling TypeScript to provide type safety and autocomplete without requiring the actual implementation.