A module is a file that contains code (variables, functions, classes) and can export or import them to share with other files.
Click to reveal answer
beginner
How do you declare a module in TypeScript?
You declare a module by creating a file and using export to share parts of the code, and import to use code from other modules.
Click to reveal answer
intermediate
What is the difference between a global script and a module in TypeScript?
A global script shares variables globally without imports/exports. A module has its own scope and shares code only through exports and imports.
Click to reveal answer
intermediate
What does the declare module syntax do in TypeScript?
It tells TypeScript about the shape of a module that exists elsewhere, often used to describe modules without TypeScript code (like JavaScript libraries).
Click to reveal answer
beginner
How do you export a function named greet from a module?
Use <code>export function greet() { ... }</code> to make the function available to other modules.
Click to reveal answer
Which keyword is used to share code from a TypeScript module?
Aexport
Bshare
Cinclude
Dmodule
✗ Incorrect
The export keyword is used to share code from a module.
How do you use code from another module in TypeScript?
Aimport
Brequire
Cinclude
Duse
✗ Incorrect
The import keyword is used to bring code from another module.
What does declare module 'xyz' do?
ARuns the module 'xyz'
BImports the module named 'xyz'
CDefines the shape of an external module for TypeScript
DExports all code from module 'xyz'
✗ Incorrect
It declares the type information for an external module.
Which of these is true about modules in TypeScript?
AModules are the same as classes
BModules share variables globally by default
CModules cannot export functions
DModules have their own scope
✗ Incorrect
Modules have their own scope and share code only via exports.
What file extension is typically used for a TypeScript module?
A.js
B.ts
C.json
D.tsx
✗ Incorrect
TypeScript modules usually use the .ts extension.
Explain how to declare and use a module in TypeScript.
Think about how you share and use code between files.
You got /4 concepts.
Describe the purpose of the declare module statement in TypeScript.
It helps TypeScript know about modules without code.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using declare module in TypeScript?
easy
A. To import modules dynamically at runtime
B. To create new modules with implementation
C. To execute JavaScript code inside TypeScript files
D. To tell TypeScript about the shape of a module without actual code
Solution
Step 1: Understand the role of declare module
This keyword is used to describe the shape or types of a module when TypeScript has no information about it.
Step 2: Differentiate from other module actions
It does not import, execute, or create code but only declares types for existing modules.
Final Answer:
To tell TypeScript about the shape of a module without actual code -> Option D
Quick Check:
declare module = describe types only [OK]
Hint: Declare modules to describe types, not to run code [OK]
Common Mistakes:
Thinking declare module imports or runs code
Confusing declare module with creating actual modules
Using declare module to execute JavaScript
2. Which of the following is the correct syntax to declare a module named "my-lib" in TypeScript?
easy
A. declare module 'my-lib' { export function foo(): void; }
B. module declare 'my-lib' { export function foo(): void; }
C. declare 'my-lib' module { export function foo(): void; }
D. module 'my-lib' declare { export function foo(): void; }
Solution
Step 1: Recall correct syntax for module declaration
The correct syntax starts with declare module 'module-name' followed by curly braces containing declarations.
Step 2: Check each option's order and keywords
Only declare module 'my-lib' { export function foo(): void; } uses the correct order: declare module 'my-lib' { ... }. Others mix keywords incorrectly.
Final Answer:
declare module 'my-lib' { export function foo(): void; } -> Option A
Quick Check:
declare module 'name' { ... } syntax [OK]
Hint: Start with 'declare module' then module name in quotes [OK]
Module names must be string literals in quotes, like 'myLib', not bare identifiers.
Step 2: Verify export syntax
Exporting constants is allowed, so no error there.
Final Answer:
Module name must be in quotes -> Option C
Quick Check:
Module names need quotes [OK]
Hint: Always put module names in quotes in declare module [OK]
Common Mistakes:
Omitting quotes around module name
Thinking export const is invalid
Assuming function declaration is mandatory
5. You want to declare a module "config" that exports an object with optional properties apiUrl (string) and timeout (number). Which declaration correctly models this?
hard
A. declare module 'config' {
export const settings: { apiUrl?: string; timeout?: number };
}
B. declare module 'config' {
export const settings: { apiUrl: string; timeout: number };
}
C. declare module 'config' {
export function settings(): { apiUrl?: string; timeout?: number };
}
D. declare module 'config' {
export let settings: { apiUrl: string; timeout?: number };
}
Solution
Step 1: Understand optional properties syntax
Optional properties use the ? symbol after the property name.
Step 2: Match declaration to requirements
declare module 'config' {
export const settings: { apiUrl?: string; timeout?: number };
} correctly declares settings as an exported constant object with both properties optional.
Step 3: Check other options
declare module 'config' {
export const settings: { apiUrl: string; timeout: number };
} requires both properties (not optional). declare module 'config' {
export function settings(): { apiUrl?: string; timeout?: number };
} exports a function, not an object. declare module 'config' {
export let settings: { apiUrl: string; timeout?: number };
} makes apiUrl required, which is incorrect.
Final Answer:
declare module 'config' {
export const settings: { apiUrl?: string; timeout?: number };
} -> Option A
Quick Check:
Optional props use ? in type declarations [OK]
Hint: Use ? after property name for optional fields [OK]
Common Mistakes:
Forgetting ? for optional properties
Using function instead of object export
Making required properties when optional needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.