0
0
Typescriptprogramming~20 mins

Namespace merging in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Namespace Master
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 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 + "!");
ATypeError: MyNamespace.greet is not a function
BReferenceError: MyNamespace is not defined
C"Hello World!"
D"Hello, World!"
Attempts:
2 left
💡 Hint
Remember that namespaces with the same name merge their exported members.
Predict Output
intermediate
2: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);
Aundefined
B5
CTypeError: Cannot read property 'id' of undefined
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Interfaces do not exist at runtime but help with type checking.
Predict Output
advanced
2: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);
AReferenceError: Circle is not defined
Bundefined
C3
DTypeError: Shapes.createCircle is not a function
Attempts:
2 left
💡 Hint
Merged namespaces share the same scope for exported members.
Predict Output
advanced
2: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());
A20
BReferenceError: value is not defined
C10
DTypeError: Outer.Inner.getValue is not a function
Attempts:
2 left
💡 Hint
Check variable scope inside nested namespaces.
🧠 Conceptual
expert
3: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?
AIn value context, Example.T is 123; in type context, Example.T is string.
BExample.T is always 123 in both value and type contexts.
CExample.T is always string in both value and type contexts.
DThis causes a compile-time error due to name conflict.
Attempts:
2 left
💡 Hint
Types and values live in separate namespaces in TypeScript.