Creating a Namespace in TypeScript Declaration Files
Learn how to create a namespace in TypeScript declaration files to group functions and properties of classes together.
We'll cover the following...
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
:
declare module ErrorHelper {function containsErrors(response: any): boolean; // checks if response contains any errorsfunction 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 ...