0
0
Typescriptprogramming~20 mins

Declaring modules in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Module Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple declared module
What is the output when running this TypeScript code that uses a declared module?
Typescript
declare module 'mathUtils' {
  export function square(x: number): number;
}

import { square } from 'mathUtils';

console.log(square(4));
A16
Bundefined
CError: Cannot find module 'mathUtils'
D4
Attempts:
2 left
💡 Hint
Declared modules only provide type information and do not include implementation.
🧠 Conceptual
intermediate
1:30remaining
Purpose of 'declare module' in TypeScript
What is the main purpose of using declare module in TypeScript?
ATo provide type information for modules without implementations
BTo create a new JavaScript module at runtime
CTo import modules dynamically
DTo export variables from a module
Attempts:
2 left
💡 Hint
Think about how TypeScript handles external JavaScript libraries without type definitions.
🔧 Debug
advanced
2:00remaining
Why does this declared module cause a type error?
Given this declared module and usage, why does TypeScript report an error?
Typescript
declare module 'config' {
  export const port: string;
}

import { port } from 'config';

const serverPort: number = port;
AType 'string' is not assignable to type 'number'
BMissing export in module 'config'
CCannot redeclare block-scoped variable 'port'
DModule 'config' not found
Attempts:
2 left
💡 Hint
Check the declared type of port and the variable it is assigned to.
📝 Syntax
advanced
2:00remaining
Correct syntax for declaring a module with a default export
Which option correctly declares a module with a default export of a function named greet?
A
declare module 'hello' {
  default export function greet(name: string): string;
}
B
declare module 'hello' {
  export default function greet(name: string): string;
}
C
declare module 'hello' {
  export function default greet(name: string): string;
}
D
declare module 'hello' {
  export = function greet(name: string): string;
}
Attempts:
2 left
💡 Hint
Remember the order of keywords for default exports in declarations.
🚀 Application
expert
1:30remaining
Number of properties in a declared module's interface
Given this declared module, how many properties does the interface Settings have?
Typescript
declare module 'appSettings' {
  interface Settings {
    theme: string;
    version: number;
    debug: boolean;
  }
  export const config: Settings;
}
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Count all properties listed inside the interface.