0
0
Typescriptprogramming~20 mins

Why modules are needed in TypeScript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
TypeScript Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
๐Ÿง  Conceptual
intermediate
2:00remaining
Purpose of Modules in TypeScript

Why do we use modules in TypeScript?

ATo allow TypeScript to run directly in the browser without compilation
BTo make the code run faster by compiling it into machine code
CTo automatically fix syntax errors in the code
DTo organize code into reusable pieces and avoid name conflicts
Attempts:
2 left
๐Ÿ’ก Hint

Think about how big projects keep code neat and avoid confusion.

โ“ Predict Output
intermediate
2:00remaining
Output of Module Variable Access

What will be the output of this TypeScript code using modules?

Typescript
export const greeting = 'Hello';

import { greeting } from './module';
console.log(greeting);
AHello
Bundefined
CReferenceError
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint

Imported variables keep their values from the module.

โ“ Predict Output
advanced
2:00remaining
Effect of Missing Module Export

What error occurs when trying to import a variable that is not exported from a module?

Typescript
const secret = 'hidden';

import { secret } from './module';
console.log(secret);
Aundefined
BSyntaxError: Unexpected token import
CError: Module './module' has no exported member 'secret'
DReferenceError: secret is not defined
Attempts:
2 left
๐Ÿ’ก Hint

Check if the variable is exported before importing.

๐Ÿง  Conceptual
advanced
2:00remaining
How Modules Help Avoid Global Scope Pollution

How do modules help prevent global scope pollution in TypeScript?

ABy converting all variables to global constants
BBy wrapping code in a private scope so variables donโ€™t leak globally
CBy running all code in the browserโ€™s global window object
DBy automatically deleting unused variables from the global scope
Attempts:
2 left
๐Ÿ’ก Hint

Think about how modules isolate code.

๐Ÿš€ Application
expert
3:00remaining
Number of Items in a Module Export Object

Given this module code, how many items will the imported object have?

Typescript
export const a = 1;
export const b = 2;
const c = 3;
export { c as d };
A3
B2
C1
D4
Attempts:
2 left
๐Ÿ’ก Hint

Count all exported names including renamed exports.