← Back to TypeScript Course | Chapter 11: Type Declarations and DefinitelyTyped | Lesson 3 of 5

@types Packages का उपयोग

@types packages उन JavaScript libraries के लिए TypeScript declarations प्रदान करती हैं जो अपनी खुद की types शामिल नहीं करती। वे TypeScript को उन libraries के लिए checking और editor support देने देती हैं।
Syntax
typescript
npm install --save-dev @types/package-name

@types Packages क्या हैं

@types packages community-maintained declaration files हैं जो उन JavaScript libraries के लिए npm पर separately publish की जाती हैं जो अपनी own TypeScript types ship नहीं करती, DefinitelyTyped project के under collectively hosted।

उदाहरण: What @types Packages Are

typescript
// npm install --save-dev @types/lodash
// Provides types for the lodash JS library, published under DefinitelyTyped.
console.log("@types packages add compile-time types for untyped JS libraries");

एक Type Package Install करना

एक type package install करना (जैसे npm install --save-dev @types/lodash) आपके runtime bundle में कुछ नहीं जोड़ता — यह सिर्फ compile-time type information जोड़ता है जो TypeScript automatically उस library के साथ pick up करता है जिसे यह type करती है।

उदाहरण: Installing a Type Package

typescript
// npm install --save-dev @types/express
// Adds zero runtime code -- only type information used by the compiler.
console.log("Installing @types/express adds no runtime bundle weight");

Typed Library APIs उपयोग करना

एक बार @types package installed हो जाए, उस library के functions में call करना पूरी autocomplete और type-checking देता है जैसे कि library शुरू से TypeScript में लिखी गई हो, कुछ भी run करने से पहले mismatched arguments पकड़ते हुए।

उदाहरण: Using Typed Library APIs

typescript
// After installing @types/lodash:
// import _ from "lodash";
// _.chunk([1, 2, 3], 2); // fully typed and autocompleted
console.log("Typed library calls get full autocomplete and checking");

Built-In Types वाली Libraries

कुछ libraries अपनी खुद की types अपने package में bundled directly ship करती हैं (अक्सर इनके package.json में types या typings field से noted), जिसका मतलब है इनके लिए एक separate @types package install करना ज़रूरी नहीं है या possible भी नहीं।

उदाहरण: Libraries with Built-In Types

typescript
// axios ships its own types (see its package.json "types" field),
// so `npm install --save-dev @types/axios` is unnecessary.
console.log("Some libraries bundle their own types, skip a separate @types install");

Type Package Availability जांचना

यह मानने से पहले कि आपको एक separate @types install चाहिए, पहले library के own package में bundled types check करें — एक already-typed library के लिए एक redundant @types package install करना conflicting type definitions create कर सकता है।

उदाहरण: Checking Type Package Availability

typescript
// Check package.json for a "types" or "typings" field first.
// Installing a redundant @types package can create conflicting definitions.
console.log("Always check for bundled types before installing @types separately");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. lodash खुद install किए बिना @types/lodash install करना, क्योंकि types package में कोई runtime code नहीं है।
  2. Production के लिए @types packages को dependencies में जोड़ना जब इन्हें सिर्फ devDependencies में चाहिए।
  3. एक library के लिए @types/x install करना जो पहले से अपनी खुद की types ship करती है, जो conflicts create कर सकता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.