Summary - Modules
- TypeScript course
- Intermediate
- Lesson 53/89
- 4 min
Write a multi-file accounting package with modules.
Summary - Modules
Modules let you split and share code across files. Named exports give multiple items, default exports give a single default, re-exports create barrel files, and import type brings in type-only items. A module can hold both named and default exports, enabling flexible API design.
// hesap.ts
export interface Islem { a: number; b: number; }
export default function hesapla(i: Islem) {
return i.a + i.b;
}Practice Calculator package
Export interface Islem with properties a: number and b: number. Export hesapla as a default function with signature (i: Islem): number returning i.a + i.b. Print the result of hesapla({ a: 5, b: 7 }) with console.log.
Starter code
// Islem arayüzünü dışa aktar: a ve b number
// hesapla fonksiyonunu varsayılan olarak dışa aktar
// Islem parametreli, a+b döndürsün
// hesapla'yı nesneyle çağır, sonucu yazdırSample solution
export interface Islem {
a: number;
b: number;
}
export default function hesapla(i: Islem): number {
return i.a + i.b;
}
console.log(hesapla({ a: 5, b: 7 }));Expected output
12
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Can a module have both named and default exports at the same time?
- Yes, both can coexist Correct answer
- Only default export is allowed
- Only named export is allowed
- No, only one kind is used
Explanation: A module can have both named and default exports at the same time. Named exports are unlimited, the default export is single; together they offer a flexible API.
2 Default export
export ____ function hesapla(i: Islem): number {
return i.a + i.b;
}
export default function hesapla(i: Islem): number {
return i.a + i.b;
}
Explanation: For a default export you write export default. named, single, and primary are not valid keywords; only default carries this meaning.
3 Match module concepts
| Term | Meaning |
|---|---|
export function topla |
Named export |
export default function |
Default export |
export { } from |
Re-export |
import type { } |
Type-only import |