TypeScript और Google Maps

TypeScript, JavaScript का एक टाइप किया गया सुपरसेट है, जो सामान्य JavaScript में कंपाइल होता है. नीचे दिए गए सैंपल कोड में, TypeScript का इस्तेमाल करके Google Maps के आसान इस्तेमाल के बारे में बताया गया है:

TypeScript

async function init(): Promise<void> {
    // Import the needed libraries.
    await google.maps.importLibrary('maps');

    // Access the map.
    const mapElement = document.querySelector('gmp-map')!;
    // Access the underlying map object.
    const innerMap = mapElement.innerMap;

    console.log({ mapElement, innerMap });
}

void init();

JavaScript

async function init() {
    // Import the needed libraries.
    await google.maps.importLibrary('maps');

    // Access the map.
    const mapElement = document.querySelector('gmp-map');
    // Access the underlying map object.
    const innerMap = mapElement.innerMap;

    console.log({ mapElement, innerMap });
}

void init();

सीएसएस

/* 
 * Optional: Makes the sample page fill the window. 
 */
html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

एचटीएमएल

<html>
    <head>
        <title>Simple Map</title>

        <link rel="stylesheet" type="text/css" href="./style.css" />
        <script type="module" src="./index.js"></script>
        <script>
            // prettier-ignore
            (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
                key: "GOOGLE_MAPS_API_KEY"
            });
        </script>
    </head>

    <body>
        <gmp-map center="-34.397,150.644" zoom="8" map-id="DEMO_MAP_ID">
        </gmp-map>
    </body>
</html>
उदाहरण देखें

अपनी प्रोफ़ाइल बनाना शुरू करें

DefinitelyTyped प्रोजेक्ट एक ओपन सोर्स प्रोजेक्ट है. यह Google Maps के साथ-साथ कई पैकेज के लिए, टाइप डिक्लेरेशन फ़ाइलें मैनेज करता है. @types/google.maps पैकेज से Google Maps JavaScript डिक्लेरेशन फ़ाइलें इंस्टॉल करने के लिए, NPM का इस्तेमाल करें:

npm i -D @types/google.maps

ज़्यादा जानकारी के लिए, GitHub पर सोर्स फ़ाइलें देखें.

google.maps को tsconfig types विकल्प में जोड़ें

अगर आपकी tsconfig फ़ाइल, types कंपाइलर विकल्प सेट करती है, तो TypeScript में सिर्फ़ उस कलेक्शन में मौजूद @types पैकेज शामिल होते हैं. Google Maps के लिए TypeScript की सुविधा चालू करने के लिए, सिर्फ़ @types/google.maps को इंस्टॉल करना काफ़ी नहीं है. साथ ही, google नेमस्पेस का इस्तेमाल करने वाला कोड, इस गड़बड़ी की वजह से कंपाइल नहीं हो पाता:

Error T2503: Cannot find namespace 'google'.

types कलेक्शन में google.maps को जोड़ने के लिए:

{
  "compilerOptions": {
    "types": ["google.maps"]
  }
}

इसके अलावा, अपने प्रोजेक्ट में शामिल किसी डिक्लेरेशन फ़ाइल में यह डायरेक्टिव जोड़ें, जैसे कि vite-env.d.ts:

/// <reference types="google.maps" />

ज़्यादा जानकारी के लिए, टाइप देखें.

@types पैकेज में कॉन्फ़्लिक्ट से जुड़ी गड़बड़ी है

कुछ लाइब्रेरी, @types/google.maps के अलावा किसी अन्य पैकेज का इस्तेमाल कर सकती हैं. इससे समस्याएं हो सकती हैं. अलग-अलग टाइप की समस्याओं से बचने के लिए, skipLibCheck कंपाइलर विकल्प का इस्तेमाल करें:

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

typeRoots की जानकारी दें

Angular जैसे कुछ फ़्रेमवर्क में, typeRoots कंपाइलर विकल्प को तय करना पड़ सकता है. इससे, @types/google.maps और अन्य सभी @types पैकेज से इंस्टॉल किए गए टाइप शामिल किए जा सकते हैं.

डिफ़ॉल्ट रूप से, दिखने वाले सभी @types पैकेज आपके कंपाइलेशन में शामिल होते हैं. किसी भी एनक्लोज़िंग फ़ोल्डर के node_modules/@types में मौजूद पैकेज को विज़िबल माना जाता है:

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types"
    ]
  }
}