Challenge - 5 Problems
Namespace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of merged namespaces with functions and variables
What is the output of the following TypeScript code when compiled and run in JavaScript?
Typescript
namespace MyNamespace {
export function greet() {
return "Hello";
}
}
namespace MyNamespace {
export const name = "World";
}
console.log(MyNamespace.greet() + ", " + MyNamespace.name + "!");Attempts:
2 left
💡 Hint
Remember that namespaces with the same name merge their exported members.
✗ Incorrect
In TypeScript, multiple namespace declarations with the same name merge into one. So, the function greet and the constant name both exist in MyNamespace. Calling greet() returns "Hello" and accessing name returns "World". The output concatenates these with a comma and exclamation mark.
❓ Predict Output
intermediate2:00remaining
Merged namespace with interface and variable
What is the output of this TypeScript code after compilation and execution?
Typescript
namespace Data {
export interface Item {
id: number;
}
}
namespace Data {
export const item: Item = { id: 5 };
}
console.log(Data.item.id);Attempts:
2 left
💡 Hint
Interfaces do not exist at runtime but help with type checking.
✗ Incorrect
The interface Item is merged into the Data namespace and used to type the variable item. The variable item exists at runtime with id 5, so logging Data.item.id outputs 5.
❓ Predict Output
advanced2:00remaining
Namespace merging with class and function
What will this TypeScript code output when run?
Typescript
namespace Shapes {
export class Circle {
constructor(public radius: number) {}
}
}
namespace Shapes {
export function createCircle(radius: number) {
return new Circle(radius);
}
}
const c = Shapes.createCircle(3);
console.log(c.radius);Attempts:
2 left
💡 Hint
Merged namespaces share the same scope for exported members.
✗ Incorrect
The class Circle and function createCircle are merged into the Shapes namespace. The function can access Circle directly. The created Circle instance has radius 3, so logging c.radius outputs 3.
❓ Predict Output
advanced2:00remaining
Namespace merging with nested namespaces
What is the output of this code snippet?
Typescript
namespace Outer {
export namespace Inner {
export const value = 10;
}
}
namespace Outer {
export namespace Inner {
export function getValue() {
return Outer.Inner.value * 2;
}
}
}
console.log(Outer.Inner.getValue());Attempts:
2 left
💡 Hint
Check variable scope inside nested namespaces.
✗ Incorrect
Inside the function getValue, the variable value is not directly accessible because it is not prefixed with Outer.Inner. or this. By using Outer.Inner.value, the function correctly accesses the value. This outputs 20.
🧠 Conceptual
expert3:00remaining
Effect of namespace merging on types and values
Consider these two namespace declarations in TypeScript:
namespace Example {
export type T = string;
}
namespace Example {
export const T = 123;
}
What happens when you try to use Example.T in a value context and a type context?
Attempts:
2 left
💡 Hint
Types and values live in separate namespaces in TypeScript.
✗ Incorrect
TypeScript allows merging of namespaces where a type and a value can share the same name. The type Example.T refers to the string type, while the value Example.T refers to the number 123. They coexist without conflict because types and values are separate.