Challenge - 5 Problems
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of merged namespaces with functions
What is the output of the following TypeScript code when compiled and run in JavaScript?
Typescript
namespace Utils {
export function greet() {
return "Hello";
}
}
namespace Utils {
export function farewell() {
return "Goodbye";
}
}
console.log(Utils.greet() + ", " + Utils.farewell() + "!");Attempts:
2 left
💡 Hint
Think about how TypeScript merges namespaces with the same name.
✗ Incorrect
In TypeScript, namespaces with the same name merge their contents. Both greet and farewell functions become part of Utils, so calling Utils.greet() and Utils.farewell() works as expected.
❓ Predict Output
intermediate2:00remaining
Value of merged namespace property
Given the following TypeScript code, what is the value of Utils.version after execution?
Typescript
namespace Utils {
export const version = "1.0";
}
namespace Utils {
export const version = "2.0";
}
console.log(Utils.version);Attempts:
2 left
💡 Hint
Later declarations overwrite earlier ones in merged namespaces.
✗ Incorrect
When namespaces merge, if they export variables with the same name, the last declaration overwrites the previous one. So Utils.version is "2.0".
🔧 Debug
advanced2:00remaining
Why does this merged namespace code cause an error?
Consider this TypeScript code snippet. Why does it cause a compilation error?
Typescript
namespace Data {
export interface Item {
id: number;
}
}
namespace Data {
export interface Item {
name: string;
}
}
const item: Data.Item = { id: 1, name: "Book" };Attempts:
2 left
💡 Hint
Think about how TypeScript merges interfaces with the same name.
✗ Incorrect
TypeScript merges interfaces with the same name by combining their properties. The merged interface Item has both id and name properties, so the object literal is valid.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in namespace merging?
Which of the following namespace declarations will cause a syntax error in TypeScript?
Attempts:
2 left
💡 Hint
Check if duplicate exported functions with the same name cause errors.
✗ Incorrect
Declaring two exported functions with the same name in merged namespaces causes a duplicate identifier error. Other options are valid because variables or interfaces merge or are scoped differently.
🚀 Application
expert2:00remaining
Number of properties in merged namespace object
After the following TypeScript code is compiled and run, how many enumerable properties does the object Utils have?
Typescript
namespace Utils {
export const a = 1;
export const b = 2;
}
namespace Utils {
export const c = 3;
export function d() { return 4; }
}
const keys = Object.keys(Utils);
console.log(keys.length);Attempts:
2 left
💡 Hint
Functions are not enumerable properties on the namespace object.
✗ Incorrect
In TypeScript, exported constants become enumerable properties on the namespace object. Exported functions are not enumerable. So keys are ['a', 'b', 'c'], length 3.