Re-exporting modules in Typescript - Time & Space Complexity
When we re-export modules in TypeScript, we combine or pass along code from one file to another.
We want to know how the time to process these exports grows as we add more modules.
Analyze the time complexity of the following code snippet.
// fileA.ts
export const a = 1;
// fileB.ts
export { a } from './fileA';
// fileC.ts
export { a } from './fileB';
// main.ts
import { a } from './fileC';
console.log(a);
This code re-exports a constant through multiple files before importing it in the main file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The module system resolves each re-export step by step.
- How many times: Once per re-exported module in the chain.
Each additional re-export adds one more step to resolve the final import.
| Input Size (number of re-exports) | Approx. Operations |
|---|---|
| 1 | 1 step |
| 10 | 10 steps |
| 100 | 100 steps |
Pattern observation: The time grows linearly with the number of re-export steps.
Time Complexity: O(n)
This means the time to resolve imports grows directly with the number of re-exported modules.
[X] Wrong: "Re-exporting many modules happens instantly, no extra time needed."
[OK] Correct: Each re-export adds a step the system must follow, so more re-exports mean more work.
Understanding how module re-exports affect time helps you write clear code and reason about build times and performance.
What if we combined all exports into one file instead of re-exporting through many files? How would the time complexity change?
Practice
Solution
Step 1: Understand re-exporting concept
Re-exporting means exporting items from one module through another to simplify imports.Step 2: Identify the main benefit
This helps organize code and makes importing easier by centralizing exports.Final Answer:
To organize and simplify imports by sharing exports from one place -> Option AQuick Check:
Re-exporting = Simplify imports [OK]
- Thinking re-exporting creates new variables
- Confusing re-exporting with running code
- Believing it prevents multiple imports
utils.ts?Solution
Step 1: Recall correct re-export syntax
To re-export all exports from a module, useexport * from 'module';.Step 2: Match syntax with options
export * from './utils'; matches the correct syntax exactly.Final Answer:
export * from './utils'; -> Option BQuick Check:
Re-export all = export * from [OK]
- Using 'import * from' instead of 'export * from'
- Writing 'export all from' which is invalid
- Using 're-export' keyword which doesn't exist
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);Solution
Step 1: Analyze exports in index.ts
Onlypiis re-exported frommathUtils.ts.eis not exported.Step 2: Check import in main.ts
Trying to import bothpiandefromindex.tscauses an error becauseeis missing.Final Answer:
Error: 'e' is not exported from './index' -> Option DQuick Check:
Missing export causes import error [OK]
- Assuming all original exports are re-exported
- Expecting undefined instead of error
- Ignoring missing export errors
// helpers.ts
export function greet() { return 'Hello'; }
// index.ts
export { greet } from './helpers';
export { greet } from './helpers';Solution
Step 1: Check for duplicate exports
The code exportsgreettwice from the same module, causing a duplicate export.Step 2: Understand TypeScript rules
Duplicate exports of the same name cause syntax errors because they conflict.Final Answer:
Duplicate export of 'greet' causes a syntax error -> Option AQuick Check:
Duplicate exports = syntax error [OK]
- Ignoring duplicate export errors
- Thinking missing semicolons cause error
- Believing functions can't be re-exported
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?Solution
Step 1: Understand renaming during re-export
You can rename exports usingexport { originalName as newName }syntax.Step 2: Apply to given modules
Re-exportredasprimaryRedfromcolors.tsandcircleasroundShapefromshapes.ts.Final Answer:
export { red as primaryRed } from './colors'; export { circle as roundShape } from './shapes'; -> Option CQuick Check:
Rename exports with 'as' keyword [OK]
- Using import instead of export for re-exporting
- Trying to export multiple modules in one statement incorrectly
- Not renaming exports properly
Ready to prove it?
Pass the test and this pattern moves unknown
