0
0
Typescriptprogramming~10 mins

Declaration merging for namespaces in Typescript - Interactive Code Practice

Choose your learning style9 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.