CodeBlanks Logo Icon CodeBlanks

Exporting types

Export public types from a barrel file.

What is a barrel file?

A barrel file (usually index.ts) re-exports multiple modules from a single place. The consumer imports from one path only: import { x } from "./lib". This hides the internal file structure and simplifies the API.

// index.ts (barrel)
export { Book } from "./types";
export { addBook } from "./helpers";

Type-only re-export

For type-only exports, export type is used. This tells the compiler that the value does not exist at runtime, only as type information. When isolatedModules is on, types must be specified separately.

export type { Book } from "./types";
export { addBook } from "./helpers";

Practice Library barrel object

The function kitapEkle(ad: string): string returns ad + " eklendi". The function kitapAra(ad: string): string returns ad + " bulundu". The Kutuphane object contains both. Print Kutuphane.kitapEkle("TS") with console.log.

Starter code

index.ts
// kitapEkle ve kitapAra fonksiyonlarını tanımla
// Kutuphane nesnesi ikisini de içersin
// Kutuphane.kitapEkle("TS") sonucunu yazdır

Sample solution

index.ts
function kitapEkle(ad: string): string {
  return ad + " eklendi";
}
function kitapAra(ad: string): string {
  return ad + " bulundu";
}
const Kutuphane = { kitapEkle, kitapAra };
console.log(Kutuphane.kitapEkle("TS"));

Expected output

TS eklendi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main purpose of a barrel file?

  • To aggregate multiple module exports in one place Correct answer
  • To hide TypeScript errors
  • To increase compilation speed
  • To reduce runtime code

Explanation: A barrel file aggregates exports from multiple modules in one file; the consumer uses a single import path.

2 Re-export

Question
// index.ts
____ { kitapEkle } from "./helpers";
Answer
// index.ts
export { kitapEkle } from "./helpers";

Explanation: To re-export a value from another module, use the export { } from syntax.

3 Barrel concepts

Term Meaning
barrel Aggregated re-export
index.ts Barrel file name
export { } from Re-export
export type Type-only export

All lessons in Intermediate