Re-export
- TypeScript course
- Intermediate
- Lesson 51/89
- 4 min
Learn to re-export modules.
Re-export
Re-export passes an item from a module directly through another module without importing it first. The syntax export { name } from "./mod" forwards the item. A barrel file gathers multiple modules behind a single entry point, simplifying import paths.
// index.ts
export { topla } from "./matematik";
export { cikar } from "./cikarma";
// app.ts
import { topla, cikar } from "./index";Barrel files
A barrel file, usually named index.ts, exports all items from modules in a folder from a single point. The consumer imports only the barrel instead of listing each module. This shortens import paths and simplifies module organization.
Practice Export with barrel pattern
Define topla with signature (a: number, b: number): number returning a + b. Define cikar with signature (a: number, b: number): number returning a - b. Export both using the barrel pattern with export { }. Print topla(10, 3) and cikar(10, 3) on separate lines.
Starter code
// topla fonksiyonunu tanımla (henüz dışa aktarma yok)
// cikar fonksiyonunu tanımla (henüz dışa aktarma yok)
// barrel desenini kullanarak ikisini dışa aktar
// her birinin sonucunu ayrı satırda yazdırSample solution
function topla(a: number, b: number): number {
return a + b;
}
function cikar(a: number, b: number): number {
return a - b;
}
export { topla, cikar };
console.log(topla(10, 3));
console.log(cikar(10, 3));Expected output
13 7
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which of the following is the re-export syntax?
- export { topla } from "./matematik" Correct answer
- import topla from "./matematik"
- import { topla } from "./matematik"
- export default topla
Explanation: Re-export is the export { } from syntax; it forwards an item from a module directly. import { } from is an import; export default is a default export; import without braces is a default import.
2 Add from
export { topla } ____ "./matematik";
export { topla } from "./matematik";
Explanation: In the re-export syntax you use the from keyword to specify the module path. import is for importing; in and path are not part of TypeScript module syntax.
3 Re-export parts
| Term | Meaning |
|---|---|
export { } from |
Re-export syntax |
index.ts |
Barrel file name |
export { topla, cikar } |
Multiple items re-exported |
from "./matematik" |
Source module path |