Bird
Raised Fist0
Typescriptprogramming~10 mins

Re-exporting modules in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Re-exporting modules
Import module A
↓
Re-export module A
↓
Other modules import from this re-export
↓
Access symbols from module A indirectly
Re-exporting lets one module pass along exports from another module, so other parts can import from a single place.
Execution Sample
Typescript
export * from './moduleA';

// moduleB.ts re-exports everything from moduleA

// Another file imports from moduleB
This code re-exports all exports from moduleA in moduleB, so other files can import from moduleB instead of moduleA.
Execution Table
StepActionModuleSymbols ExportedResult
1moduleA defines exportsmoduleA{ foo, bar }foo and bar available from moduleA
2moduleB re-exports from moduleAmoduleB{ foo, bar }foo and bar available from moduleB
3Other file imports foo from moduleBotherfoofoo imported successfully
4Other file uses foootherfoofoo function runs as defined in moduleA
5End--Re-exporting works as expected
💡 All exports from moduleA are available through moduleB, so importing from moduleB works.
Variable Tracker
ModuleExports StartAfter Re-exportFinal
moduleAfoo, barfoo, barfoo, bar
moduleB-foo, barfoo, bar
other-foofoo
Key Moments - 2 Insights
Why can other files import foo from moduleB even though foo is defined in moduleA?
Because moduleB re-exports foo from moduleA (see execution_table step 2), making foo available through moduleB.
Does re-exporting copy the code or just pass the exports along?
Re-exporting does not copy code; it passes along references to the original exports (see execution_table steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does moduleB gain access to foo and bar?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Symbols Exported' column for moduleB in execution_table rows.
According to variable_tracker, what exports does moduleA have after re-exporting?
Afoo and bar
BNo exports
Cfoo only
Dbar only
💡 Hint
Look at the 'Exports Start' and 'After Re-export' columns for moduleA in variable_tracker.
If moduleB did not re-export from moduleA, what would happen when other files import foo from moduleB?
Afoo would be undefined but import succeeds
BImport would succeed anyway
CImport would fail because foo is not exported by moduleB
Dfoo would be imported from moduleA automatically
💡 Hint
Refer to execution_table step 2 and 3 about re-exporting and importing.
Concept Snapshot
Re-exporting modules in TypeScript:

export * from './moduleA';

- Passes all exports from moduleA through another module
- Lets other files import from a single module
- Does not copy code, just references
- Helps organize and simplify imports
Full Transcript
Re-exporting modules means one module passes along exports from another module. For example, moduleB can re-export everything from moduleA using export * from './moduleA';. This way, other files can import symbols like foo from moduleB instead of moduleA. The execution table shows moduleA defines foo and bar, moduleB re-exports them, and other files import foo from moduleB successfully. Variable tracker shows how exports flow from moduleA to moduleB and then to other files. Key moments clarify that re-exporting passes references, not copies, and enables simpler imports. The visual quiz tests understanding of when exports become available and what happens if re-exporting is missing.

Practice

(1/5)
1. What is the main purpose of re-exporting modules in TypeScript?
easy
A. To organize and simplify imports by sharing exports from one place
B. To create new variables inside a module
C. To run code automatically when a module loads
D. To prevent modules from being imported multiple times

Solution

  1. Step 1: Understand re-exporting concept

    Re-exporting means exporting items from one module through another to simplify imports.
  2. Step 2: Identify the main benefit

    This helps organize code and makes importing easier by centralizing exports.
  3. Final Answer:

    To organize and simplify imports by sharing exports from one place -> Option A
  4. Quick Check:

    Re-exporting = Simplify imports [OK]
Hint: Re-exporting centralizes exports for easier imports [OK]
Common Mistakes:
  • Thinking re-exporting creates new variables
  • Confusing re-exporting with running code
  • Believing it prevents multiple imports
2. Which of the following is the correct syntax to re-export everything from a module named utils.ts?
easy
A. export all from './utils';
B. export * from './utils';
C. import * from './utils';
D. re-export * from './utils';

Solution

  1. Step 1: Recall correct re-export syntax

    To re-export all exports from a module, use export * from 'module';.
  2. Step 2: Match syntax with options

    export * from './utils'; matches the correct syntax exactly.
  3. Final Answer:

    export * from './utils'; -> Option B
  4. Quick Check:

    Re-export all = export * from [OK]
Hint: Use 'export * from' to re-export all [OK]
Common Mistakes:
  • Using 'import * from' instead of 'export * from'
  • Writing 'export all from' which is invalid
  • Using 're-export' keyword which doesn't exist
3. Given the following files, what will be the output of console.log in main.ts?
// mathUtils.ts
export const pi = 3.14;
export const e = 2.71;

// index.ts
export { pi } from './mathUtils';

// main.ts
import { pi, e } from './index';
console.log(pi, e);
medium
A. 3.14 2.71
B. 3.14 undefined
C. undefined 2.71
D. Error: 'e' is not exported from './index'

Solution

  1. Step 1: Analyze exports in index.ts

    Only pi is re-exported from mathUtils.ts. e is not exported.
  2. Step 2: Check import in main.ts

    Trying to import both pi and e from index.ts causes an error because e is missing.
  3. Final Answer:

    Error: 'e' is not exported from './index' -> Option D
  4. Quick Check:

    Missing export causes import error [OK]
Hint: Only re-exported items can be imported [OK]
Common Mistakes:
  • Assuming all original exports are re-exported
  • Expecting undefined instead of error
  • Ignoring missing export errors
4. Identify the error in this re-exporting code snippet:
// helpers.ts
export function greet() { return 'Hello'; }

// index.ts
export { greet } from './helpers';
export { greet } from './helpers';
medium
A. Duplicate export of 'greet' causes a syntax error
B. Missing semicolon after export statements
C. Cannot re-export functions, only variables
D. No error, code works fine

Solution

  1. Step 1: Check for duplicate exports

    The code exports greet twice from the same module, causing a duplicate export.
  2. Step 2: Understand TypeScript rules

    Duplicate exports of the same name cause syntax errors because they conflict.
  3. Final Answer:

    Duplicate export of 'greet' causes a syntax error -> Option A
  4. Quick Check:

    Duplicate exports = syntax error [OK]
Hint: Avoid exporting same name twice in one module [OK]
Common Mistakes:
  • Ignoring duplicate export errors
  • Thinking missing semicolons cause error
  • Believing functions can't be re-exported
5. You have two modules: colors.ts exporting red and blue, and shapes.ts exporting circle and square. How can you create a single index.ts that re-exports only red and circle with new names primaryRed and roundShape?
hard
A. import { red, circle } from './colors'; export { red as primaryRed, circle as roundShape };
B. export * from './colors'; export * from './shapes';
C. export { red as primaryRed } from './colors'; export { circle as roundShape } from './shapes';
D. export { primaryRed, roundShape } from './colors', './shapes';

Solution

  1. Step 1: Understand renaming during re-export

    You can rename exports using export { originalName as newName } syntax.
  2. Step 2: Apply to given modules

    Re-export red as primaryRed from colors.ts and circle as roundShape from shapes.ts.
  3. Final Answer:

    export { red as primaryRed } from './colors'; export { circle as roundShape } from './shapes'; -> Option C
  4. Quick Check:

    Rename exports with 'as' keyword [OK]
Hint: Use 'export { oldName as newName }' to rename exports [OK]
Common Mistakes:
  • Using import instead of export for re-exporting
  • Trying to export multiple modules in one statement incorrectly
  • Not renaming exports properly
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