Default export
- TypeScript course
- Intermediate
- Lesson 50/89
- 4 min
Learn default exports.
Default export
Each module can have at most one default export. With export default you export a function, class, or object as the default. When importing you do not use curly braces; you can name it whatever you want.
// hesap.ts
export default function topla(a: number, b: number) {
return a + b;
}
// app.ts
import topla from "./hesap";
console.log(topla(2, 3)); // 5The name is flexible in default import
Since default imports have no curly braces, you can freely choose the imported name. Writing import hesapla from "./hesap" imports the module's default export under the name hesapla. Whatever the original name was, the name you give here is what counts.
Practice Default greeting function
Export selam as a default function with signature (): string and body return "merhaba". Then print the result of selam() with console.log.
Starter code
// selam fonksiyonunu varsayılan olarak dışa aktar
// parametresiz, string döndürsün
// "merhaba" döndürsün
// fonksiyonu çağırıp sonucu yazdırSample solution
export default function selam(): string {
return "merhaba";
}
console.log(selam());Expected output
merhaba
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How many default exports can a module have?
- Unlimited
- Exactly two
- Default export is not allowed
- At most one Correct answer
Explanation: Each module can have at most one default export. Named exports have no limit, but the default export is single; a second one is a compile error.
2 Add default
export ____ function selam(): string {
return "merhaba";
}
export default function selam(): string {
return "merhaba";
}
Explanation: For a default export you write export default. named, single, and primary are not valid keywords; only default carries this meaning.
3 Default export parts
| Term | Meaning |
|---|---|
export default function |
Default export declaration |
import selam from "./hesap" |
Import without curly braces |
en fazla bir tane |
Default export limit |
ad serbestçe seçilir |
Default import flexibility |