Bird
Raised Fist0
Typescriptprogramming~10 mins

Declaration merging for namespaces in Typescript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a namespace named 'Shapes'.

Typescript
namespace [1] {
  export const pi = 3.14;
}
Drag options to blanks, or click blank then click option'
AShapes
BMath
CCircle
DGeometry
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different namespace name than 'Shapes'.
2fill in blank
medium

Complete the code to add a function 'area' inside the 'Shapes' namespace.

Typescript
namespace Shapes {
  export function [1](radius: number): number {
    return pi * radius * radius;
  }
}
Drag options to blanks, or click blank then click option'
Aperimeter
Bcircumference
Cdiameter
Darea
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name unrelated to area calculation.
3fill in blank
hard

Fix the error in the code by completing the namespace merging correctly.

Typescript
namespace Shapes {
  export const color = 'red';
}

namespace [1] {
  export function getColor() {
    return color;
  }
}
Drag options to blanks, or click blank then click option'
ACircle
BShapes
CGeometry
DColors
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different namespace name prevents merging.
4fill in blank
hard

Fill both blanks to complete the merged namespace with a variable and a function accessing it.

Typescript
namespace Shapes {
  export let [1] = 'blue';
}

namespace Shapes {
  export function get[2]() {
    return [1];
  }
}
Drag options to blanks, or click blank then click option'
Acolor
BColor
Cshape
DShape
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching variable and function names.
5fill in blank
hard

Fill all three blanks to create a merged namespace with a type, a variable, and a function returning the variable.

Typescript
namespace Shapes {
  export type [1] = { radius: number };
  export let [2]: [1] = { radius: 5 };
  export function getRadius() {
    return [3].radius;
  }
}
Drag options to blanks, or click blank then click option'
ACircle
Bcircle
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent capitalization between type and variable.

Practice

(1/5)
1. What does declaration merging for namespaces in TypeScript allow you to do?
easy
A. Merge classes and interfaces automatically
B. Combine multiple namespace blocks with the same name into one namespace
C. Create multiple namespaces with different names in one file
D. Prevent namespaces from being extended

Solution

  1. Step 1: Understand declaration merging concept

    Declaration merging means combining multiple declarations of the same name into one.
  2. Step 2: Apply to namespaces

    For namespaces, this means multiple blocks with the same namespace name merge into a single namespace.
  3. Final Answer:

    Combine multiple namespace blocks with the same name into one namespace -> Option B
  4. Quick Check:

    Declaration merging = Combine namespaces [OK]
Hint: Namespaces with same name merge into one combined namespace [OK]
Common Mistakes:
  • Thinking namespaces with different names merge
  • Confusing merging with class or interface merging
  • Believing merging prevents extension
2. Which of the following is the correct syntax to merge two namespace declarations named Utils?
easy
A. namespace Utils { export function a() {} } namespace Utils { export function b() {} }
B. namespace Utils { export function a() {} } namespace Utils2 { export function b() {} }
C. namespace Utils { export function a() {} } class Utils { }
D. namespace Utils { export function a() {} } interface Utils { }

Solution

  1. Step 1: Check namespace names

    Only namespaces with the exact same name merge. namespace Utils { export function a() {} } namespace Utils { export function b() {} } uses "Utils" twice.
  2. Step 2: Verify syntax correctness

    namespace Utils { export function a() {} } namespace Utils { export function b() {} } correctly declares two namespace blocks with functions inside, which will merge.
  3. Final Answer:

    namespace Utils { export function a() {} } namespace Utils { export function b() {} } -> Option A
  4. Quick Check:

    Same namespace name + multiple blocks = merge [OK]
Hint: Use identical namespace names to merge declarations [OK]
Common Mistakes:
  • Using different namespace names expecting merge
  • Mixing namespace with class or interface of same name
  • Incorrect syntax for namespace blocks
3. What will be the output of the following TypeScript code?
namespace Data {
  export const value = 10;
}
namespace Data {
  export function getValue() { return value; }
}
console.log(Data.getValue());
medium
A. 10
B. undefined
C. ReferenceError
D. SyntaxError

Solution

  1. Step 1: Understand namespace merging

    Both namespace blocks named "Data" merge into one namespace containing both value and getValue.
  2. Step 2: Evaluate function call

    getValue() returns value which is 10, so console.log prints 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Merged namespace members accessible = 10 [OK]
Hint: Merged namespaces share members, so functions access variables [OK]
Common Mistakes:
  • Assuming separate namespaces, so value is undefined
  • Expecting runtime errors due to merging
  • Confusing variable scope inside merged namespaces
4. Identify the error in the following code that attempts to merge namespaces:
namespace Tools {
  export function toolA() { return 'A'; }
}
namespace Tools {
  const secret = 'hidden';
  export function toolB() { return secret; }
}
medium
A. Functions cannot be declared in merged namespaces
B. Duplicate namespace name causes syntax error
C. Cannot access non-exported 'secret' in merged namespace
D. 'secret' must be exported to be used inside toolB

Solution

  1. Step 1: Check variable scope in merged namespaces

    The variable secret is declared but not exported, so it is private to its namespace block.
  2. Step 2: Analyze usage of 'secret' in toolB

    Function toolB tries to return secret, but since secret is not accessible outside its block, this causes an error.
  3. Final Answer:

    Cannot access non-exported 'secret' in merged namespace -> Option C
  4. Quick Check:

    Non-exported members are block-scoped [OK]
Hint: Non-exported members are private to their block, not merged [OK]
Common Mistakes:
  • Assuming all members merge regardless of export
  • Thinking duplicate namespace names cause syntax errors
  • Believing functions can't be declared in merged namespaces
5. Given two namespace declarations below, what will console.log(Shapes.area(5)); output?
namespace Shapes {
  export function area(radius: number) {
    return Math.PI * radius * radius;
  }
}
namespace Shapes {
  export function perimeter(radius: number) {
    return 2 * Math.PI * radius;
  }
  export function area(radius: number) {
    return Math.round(Math.PI * radius * radius);
  }
}
console.log(Shapes.area(5));
hard
A. 78.53981633974483
B. Error: Duplicate function name
C. NaN
D. 79

Solution

  1. Step 1: Understand merging with duplicate function names

    When namespaces merge, later declarations override earlier ones for the same member name.
  2. Step 2: Identify which 'area' function is used

    The second namespace redefines area with rounding. So Shapes.area(5) calls the rounded version.
  3. Step 3: Calculate output

    Math.PI * 5 * 5 ≈ 78.5398, rounded is 79.
  4. Final Answer:

    79 -> Option D
  5. Quick Check:

    Later declarations override earlier ones [OK]
Hint: Later namespace members override earlier ones with same name [OK]
Common Mistakes:
  • Expecting both functions to coexist without override
  • Thinking duplicate function names cause errors
  • Ignoring rounding in second area function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard