In TypeScript, namespaces provide a way to organize your code and avoid naming collisions between different parts of your application. Namespaces allow you to group related classes, interfaces, functions, and variables under a single name.
The syntax for creating a namespace in TypeScript is as follows:
1 2 3 4 5 6 7 |
namespace NamespaceName { export class Class_Name { ... } export interface Interface_Name { ... } export function function_Name() { ... } export const constant_Name = value; ... } |
example:
1 2 3 4 5 6 7 8 |
namespace MyApp { export interface Message { color: string} export function logMessage(message: Message) { console.log(message.color) } export const color = 'RED'; } var msg:MyApp.Message={color: MyApp.color } // object of type namespace interface MyApp.logMessage(msg) // calling |
Output: RED