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?