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

Declaring Global Types and Interfaces

Expose global type aliases and interfaces that are available everywhere without imports.

Declaring Global Types and Interfaces

What you'll learn: How to define types and interfaces that are accessible everywhere in your TypeScript project without needing to import them.

The Global Type Problem

When working with declaration files, you often want to define types that are available throughout your entire codebase automatically. Instead of importing the same type in dozens of files, you can declare it globally—making it appear as if it's built into TypeScript itself.

How Global Type Declarations Work

In a .d.ts file (not a .ts file with code), if you don't use any import or export statements, TypeScript treats the entire file as ambient—meaning everything declared in it becomes globally available.

// types/global.d.ts

// Global type alias - usable anywhere without importing
type UserId = string;

// Global interface - usable anywhere without importing
interface User {
  id: UserId;
  name: string;
  email: string;
}

// Global type for API responses
type ApiResponse<T> = {
  data: T;
  status: number;
  message: string;
};

Now in any .ts file in your project:

// No imports needed!
const user: User = {
  id: "123",
  name: "Alice",
  email: "alice@example.com"
};

function fetchUser(): ApiResponse<User> {
  // UserId, User, and ApiResponse are all available globally
  return {
    data: user,
    status: 200,
    message: "Success"
  };
}

Think of It Like Built-In Types

Just as you don't need to import string, number, or Promise, your global types work the same way. They're part of the "vocabulary" TypeScript understands throughout your project.

When to Use Global Types

Global types shine for:

  • Domain models used everywhere (User, Product, Order)
  • Utility types specific to your application
  • Constants and enums that appear in many modules
  • Typing third-party globals that don't have types

Key Takeaway: Declare types and interfaces in .d.ts files without import/export statements to make them globally available everywhere in your project, eliminating repetitive imports for commonly-used types.