Declare Namespaces in TypeScript
Back to: TypeScript Tutorial
In TypeScript, namespaces are used to group related code and prevent naming conflicts. They provide a way to organize code into logical units and create a hierarchical structure for your application.
To declare a namespace in TypeScript, use the namespace
keyword followed by the name of the namespace. Inside the namespace, you can declare functions, classes, and other code elements as needed.
Declaring a Namespace in TypeScript:
Here’s an example of declaring a namespace in TypeScript:
namespace MyNamespace { export function sayHello(name: string) { console.log(`Hello, ${name}!`); } export class MyClass { private _name: string; constructor(name: string) { this._name = name; } public get name() { return this._name; } public set name(value: string) { this._name = value; } } } const myObj = new MyNamespace.MyClass("John"); MyNamespace.sayHello(myObj.name); // outputs "Hello, John!"
In this example, we declare a namespace called MyNamespace
. Inside the namespace, we define a sayHello
function that logs a greeting to the console and a MyClass
class that has a private _name
property and public get
and set
accessors for that property.
We then create a new instance of MyClass
and pass the object’s name
property to the sayHello
function.
Using Namespaces with Modules:
In TypeScript, you can use namespaces in conjunction with modules to organize your code and avoid naming conflicts. Modules provide a way to encapsulate code into separate files or directories and declare dependencies between them.
Here’s an example of using namespaces with modules:
// file1.ts namespace MyNamespace { export function sayHello(name: string) { console.log(`Hello, ${name}!`); } } // file2.ts ///<reference path="file1.ts"/> namespace MyNamespace { export class MyClass { private _name: string; constructor(name: string) { this._name = name; } public get name() { return this._name; } public set name(value: string) { this._name = value; } } } // app.ts import { MyNamespace } from "./file2"; const myObj = new MyNamespace.MyClass("John"); MyNamespace.sayHello(myObj.name); // outputs "Hello, John!"
In this example, we have three files: file1.ts
, file2.ts
, and app.ts
. file1.ts
declares the MyNamespace
namespace and the sayHello
function inside it.
file2.ts
references file1.ts
and declares the MyNamespace
namespace again, this time including the MyClass
class inside it.
Finally, app.ts
imports the MyNamespace
namespace from file2.ts
and uses it to create a new instance of MyClass
and call the sayHello
function.
Top FAQs on Namespaces in TypeScript:
- What is a namespace in TypeScript?
- How do you declare a namespace in TypeScript?
- What is the difference between a namespace and a module in TypeScript?
- How do you reference a namespace in TypeScript?
- What are some best practices for using namespaces in TypeScript?
In conclusion, understanding closures is essential for writing efficient and effective JavaScript code. Closures are a powerful feature of the language that allows functions to maintain access to their surrounding state, even after they have been called. This allows for the creation of private variables and the implementation of callbacks, among other things.
By understanding how closures work, you can optimize your code and create more modular and maintainable applications. Some common use cases for closures include event handling, memoization, and currying.
Overall, closures are a fundamental concept in JavaScript that every developer should understand. By taking the time to learn and master closures, you can unlock the full potential of the language and write better code.