TypeScript के साथ Library
In this page:
// tsconfig.json: "declaration": true, "outDir": "dist"
export function functionName(param: ParamType): ReturnType {
// ...
}
Core Concept
TypeScript में एक publishable library लिखने का मतलब है कि compiler runtime .js output और .d.ts declaration files दोनों generate करता है, इसलिए जो भी आपका package install करे उसे बिना आपके source की ज़रूरत के free में पूरी type information मिलती है।
उदाहरण: Core Concept
export function parseDate(input: string): Date {
return new Date(input);
}
console.log(parseDate("2024-01-01"));
Basic Setup
एक basic setup tsconfig.json में "declaration": true और "outDir": "dist" set करता है, और package.json का "types" field emitted .d.ts entry file की ओर point करता है इसलिए consumers के editors इसे automatically pick up करते हैं।
उदाहरण: Basic Setup
// tsconfig.json: { "compilerOptions": { "declaration": true, "outDir": "dist" } }
// package.json: { "types": "dist/index.d.ts" }
console.log("declaration output ships .d.ts files alongside compiled JS");
Typed Example
एक typed example: अपनी library से export function parseDate(input: string): Date export करने का मतलब है कि एक consumer का editor hover पर exact parameter और return types दिखाता है, बिना किसी separate @types package की ज़रूरत के।
उदाहरण: Typed Example
export function parseDate(input: string): Date {
return new Date(input);
}
// A consumer's editor shows this exact signature on hover, no @types needed.
console.log(parseDate("2024-06-15").getFullYear());
Project Usage
एक असली project में, अपनी library की public API surface को deliberately छोटी और well-typed रखना (हर internal helper export करने के बजाय) future changes को आसान बनाता है, क्योंकि जो export नहीं किया गया उसे consumers को तोड़े बिना freely refactor किया जा सकता है।
उदाहरण: Project Usage
// Only export the small, intentional public surface:
export function formatCurrency(amount: number): string {
return `$${amount.toFixed(2)}`;
}
// internalHelper() stays unexported and freely refactorable.
console.log(formatCurrency(19.99));
Best Practices
अपनी library की own test suite कभी-कभी *compiled* .d.ts output के against चलाएं (सिर्फ source नहीं), क्योंकि एक valid-looking source file कभी-कभी ऐसा declaration output produce कर सकती है जो आपके intent से असल में match न करे।
उदाहरण: Best Practices
export interface LibraryConfig {
apiKey: string;
}
export function configure(config: LibraryConfig): void {
console.log("Configured with", config.apiKey);
}
// Occasionally test against the compiled .d.ts output, not just source.
configure({ apiKey: "abc" });
"declaration": trueभूल जाना, इसलिए consumers को कोई.d.tstypes नहीं मिलतीं।package.jsonमेंmainयाtypesकोsrc/की ओर point करना, builtdist/folder के बजाय।- हर internal helper export करना, जो public API को बाद में बदलना मुश्किल बनाता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next