@types Packages का उपयोग
In this page:
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
// 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
// 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
// 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
// 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
// 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");
lodashखुद install किए बिना@types/lodashinstall करना, क्योंकि types package में कोई runtime code नहीं है।- Production के लिए
@typespackages कोdependenciesमें जोड़ना जब इन्हें सिर्फdevDependenciesमें चाहिए। - एक library के लिए
@types/xinstall करना जो पहले से अपनी खुद की types ship करती है, जो conflicts create कर सकता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: