Middle
What is a namespace in TypeScript?
sobes.tech AI
Answer from AI
Namespace in TypeScript is a way to logically group code, such as functions, classes, interfaces, to avoid name conflicts and improve code organization.
A namespace is created using the namespace keyword and allows encapsulating related entities within a single namespace.
Example:
namespace Utils {
export function log(message: string) {
console.log(message);
}
}
Utils.log("Hello from namespace!");
Here, the log function is inside the Utils namespace, and to call it, you need to use the qualified name Utils.log.
Note: In modern projects, ES6 modules are more commonly used instead of namespaces, but namespaces are still useful for organizing code in some cases.