Package design
- TypeScript course
- Intermediate
- Lesson 71/89
- 5 min
Set up a multi-file library skeleton.
Package design
A library package is usually built on a multi-file skeleton. Each file has a single responsibility: types, helper functions, and the entry point live in separate files. The src/index.ts file is the public door of the package and combines other modules.
src/
types.ts // type definitions
helpers.ts // helper functions
index.ts // entry pointModule structure
Each file is a module; values are exported with export and imported with import. The tsconfig.json sets compilation options and the output directory. This way, compiling the package produces a single dist/ folder.
// types.ts
export interface Book { name: string; }
// index.ts
export { Book } from "./types";Practice Book interface and add function
Define an interface named Kitap with a property ad: string. The function kitapEkle takes a title: string parameter and returns { ad: title } of type Kitap. Print kitapEkle("TypeScript").ad with console.log.
Starter code
// Kitap adında interface tanımla: ad: string
// kitapEkle fonksiyonu title alıp Kitap dönsün
// kitapEkle("TypeScript").ad sonucunu yazdırSample solution
interface Kitap {
ad: string;
}
function kitapEkle(title: string): Kitap {
return { ad: title };
}
console.log(kitapEkle("TypeScript").ad);Expected output
TypeScript
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the role of index.ts in a library package?
- Contains only type definitions
- Contains no runtime code
- Replaces tsconfig.json
- Combines modules as the package entry point Correct answer
Explanation: index.ts is the package entry point; it imports other modules and re-exports them as a single API.
2 Export keyword
// types.ts
____ interface Kitap { ad: string; }
// types.ts
export interface Kitap { ad: string; }
Explanation: The export keyword is used to export a value from a module; import brings it in.
3 Package parts
| Term | Meaning |
|---|---|
src/types.ts |
Type definitions |
src/helpers.ts |
Helper functions |
src/index.ts |
Entry point |
tsconfig.json |
Compilation settings |