Re-exporting modules lets you gather exports from different files and share them from one place. This makes your code easier to organize and use.
Re-exporting modules in Typescript
export * from './module'; export { name1, name2 } from './module'; export { originalName as newName } from './module';
export * from './module'; shares everything exported by module.
You can pick specific exports or rename them when re-exporting.
mathUtils module.export * from './mathUtils';
add and subtract functions from mathUtils.export { add, subtract } from './mathUtils';
add function but renames it to sum when exporting.export { add as sum } from './mathUtils';
We have two modules: mathUtils and stringUtils. The index.ts file re-exports all math functions and the shout function from stringUtils. In main.ts, we import everything from index.ts and use the functions.
// file: mathUtils.ts export function add(a: number, b: number) { return a + b; } export function subtract(a: number, b: number) { return a - b; } // file: stringUtils.ts export function shout(text: string) { return text.toUpperCase() + '!'; } // file: index.ts export * from './mathUtils'; export { shout } from './stringUtils'; // file: main.ts import { add, subtract, shout } from './index'; console.log(add(5, 3)); console.log(subtract(10, 4)); console.log(shout('hello'));
Re-exporting does not create new copies of the exports; it just forwards them.
Be careful to avoid circular dependencies when re-exporting modules.
You can combine re-exporting with local exports in the same file.
Re-exporting helps organize and simplify your imports by sharing exports from one place.
You can export everything, some parts, or rename exports when re-exporting.
This keeps your code cleaner and easier to maintain.
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
