0
0
Typescriptprogramming~20 mins

Declaration merging for namespaces in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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() + "!");
ATypeError: Utils.farewell is not a function
B"Hello Goodbye!"
C"Hello, Goodbye!"
DSyntaxError: Duplicate namespace declaration
Attempts:
2 left
💡 Hint
Think about how TypeScript merges namespaces with the same name.
Predict Output
intermediate
2: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);
A"2.0"
B"1.0"
CTypeError: Cannot redeclare 'version'
Dundefined
Attempts:
2 left
💡 Hint
Later declarations overwrite earlier ones in merged namespaces.
🔧 Debug
advanced
2: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" };
ANo error; interfaces merge and item is valid
BError because the object literal is missing a required property
CError because the merged interface Item has conflicting property types
DError because interfaces with the same name cannot be merged
Attempts:
2 left
💡 Hint
Think about how TypeScript merges interfaces with the same name.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in namespace merging?
Which of the following namespace declarations will cause a syntax error in TypeScript?
A
namespace N { export interface I { a: number; } }
namespace N { export interface I { b: string; } }
B
namespace N { export function f() {} }
namespace N { export function f() {} }
C
namespace N { export let x = 10; }
namespace N { let x = 20; }
D
namespace N { export const a = 1; }
namespace N { export const b = 2; }
Attempts:
2 left
💡 Hint
Check if duplicate exported functions with the same name cause errors.
🚀 Application
expert
2: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);
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Functions are not enumerable properties on the namespace object.