Search⌘ K
AI Features

Creating a Namespace in TypeScript Declaration Files

Explore how to create namespaces in TypeScript declaration files to organize class functions and properties. Understand defining interfaces for precise typing and how detailed declaration files improve IDE support by enforcing strict type rules.

When writing declaration files, we need a way of grouping functions and properties of classes under the class name. TypeScript uses the module keyword as a means of creating a namespace so that all functions or properties of a class can be grouped together within the class namespace.

Creating a namespace

Let’s take a look at the first version of a declaration file for our ErrorHelper JavaScript class in the file named globals.d.ts:

TypeScript 4.9.5
declare module ErrorHelper {
function containsErrors(response: any): boolean; // checks if response contains any errors
function trace(message: any): void; // logs a message for debugging purposes
}
Namespace declaration

Here, we use the declare keyword combined with the module keyword to define a namespace named ErrorHelper on ...