0
0
Typescriptprogramming~10 mins

Namespace declaration in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Namespace declaration
Start
Declare Namespace
Add Variables/Functions/Classes
Access Members with Namespace
Use in Code
End
This flow shows how a namespace is declared, members are added, and then accessed using the namespace name.
Execution Sample
Typescript
namespace MySpace {
  export const greeting = "Hello";
  export function sayHi() {
    return greeting + ", world!";
  }
}
console.log(MySpace.sayHi());
This code declares a namespace with a variable and a function, then calls the function using the namespace.
Execution Table
StepActionEvaluationResult
1Declare namespace MySpaceNamespace createdMySpace namespace exists
2Add exported const greetinggreeting = "Hello"MySpace.greeting = "Hello"
3Add exported function sayHiFunction definedMySpace.sayHi() available
4Call MySpace.sayHi()Execute functionReturns "Hello, world!"
5console.log outputPrint returned stringOutput: Hello, world!
💡 Execution ends after printing the greeting message.
Variable Tracker
VariableStartAfter DeclarationAfter AssignmentFinal
MySpaceundefinednamespace object createdgreeting and sayHi addedMySpace with greeting and sayHi
MySpace.greetingundefinedundefined"Hello""Hello"
MySpace.sayHiundefinedundefinedfunctionfunction
Key Moments - 2 Insights
Why do we use 'export' inside the namespace?
Without 'export', members inside the namespace are private and cannot be accessed outside. See execution_table step 2 and 3 where 'export' makes greeting and sayHi accessible.
How do we access the function sayHi outside the namespace?
We use the namespace name followed by a dot and the function name, like MySpace.sayHi(). This is shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of MySpace.greeting after step 3?
Anull
Bundefined
C"Hello"
DFunction
💡 Hint
Check variable_tracker row for MySpace.greeting after 'After Assignment' column.
At which step does the function sayHi become available inside the namespace?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table step 3 where sayHi is defined.
If we remove 'export' from greeting, what happens when calling MySpace.sayHi()?
AsayHi returns 'Hello, world!' as before
BsayHi causes an error accessing greeting
CsayHi is not accessible outside namespace
DNamespace declaration fails
💡 Hint
Non-exported members are still accessible within the same namespace.
Concept Snapshot
Namespace declaration syntax:
namespace Name {
  export const/let/var/function/class ...
}

- Use 'export' to make members accessible outside.
- Access members with NamespaceName.member.
- Helps organize code and avoid name conflicts.
Full Transcript
This example shows how to declare a namespace in TypeScript. We start by creating a namespace called MySpace. Inside, we add a constant greeting and a function sayHi, both marked with 'export' so they can be used outside. When we call MySpace.sayHi(), it returns the greeting combined with ', world!'. The console.log prints this message. Export is important because without it, the members stay private inside the namespace. Accessing members requires prefixing with the namespace name. This helps keep code organized and prevents name clashes.