Integration
- TypeScript course
- Intermediate
- Lesson 75/89
- 5 min
Build the working library by combining all parts.
Summary - Library package
In this project you combined all parts of a library package: type definitions with interface, generic helper functions, exports with a barrel file, and declarations with .d.ts. You built a coherent, type-safe, and reusable package.
interface Book { name: string; }
function first<T>(arr: T[]): T | undefined {
return arr[0];
}Final assembly
In the final assembly, index.ts imports and re-exports all modules; generic helpers serve the application; .d.ts provides types to JS consumers. Each part carries its own responsibility and together they form a coherent API.
// index.ts
export { Book, first } from "./core";Practice Library integration
The Kitap interface should have ad: string and yazar: string properties. The generic function firstElement<T>(arr: T[]): T | undefined should return arr[0]. The kitaplar array should contain two Kitap objects. Get the first book with firstElement(kitaplar) and print its .ad property with console.log.
Starter code
// Kitap interface: ad ve yazar string
// firstElement generic fonksiyonu tanımla
// kitaplar dizisi iki Kitap içersin
// ilk kitabın adını yazdırSample solution
interface Kitap {
ad: string;
yazar: string;
}
function firstElement<T>(arr: T[]): T | undefined {
return arr[0];
}
const kitaplar: Kitap[] = [
{ ad: "TypeScript", yazar: "Ahmet" },
{ ad: "React", yazar: "Mehmet" }
];
const ilk = firstElement(kitaplar);
console.log(ilk.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 most important principle during integration?
- Each part has a single responsibility Correct answer
- Skip type declarations
- Avoid generic functions
- Write all code in a single file
Explanation: In integration, each part having a single responsibility is key; types, helpers, barrel, and declarations are kept separate.
2 Import keyword
// index.ts
____ { Kitap } from "./types";
// index.ts
import { Kitap } from "./types";
Explanation: The import keyword is used to bring in a value from another module.
3 Integration parts
| Term | Meaning |
|---|---|
interface |
Type definition |
generic helper |
Type-safe helper |
barrel |
Aggregated export |
.d.ts |
Declaration file |