Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
TypeScript es un superconjunto tipado de JavaScript que se compila en JavaScript simple. En el fragmento que aparece a continuación, se demuestra el uso simple de Google Maps con TypeScript.
El proyecto DefinitelyTyped es un proyecto de código abierto en el que se encuentran los archivos de declaración de tipo de muchos paquetes, incluido Google Maps. Los archivos de declaración de JavaScript de Google Maps (consulta los archivos fuente en GitHub) se pueden instalar usando NPM desde el paquete @types/google.maps.
npmi-D@types/google.maps
Funciones alfa y beta
Por lo general, los tipos no tienen las propiedades, funciones o clases que se encuentran en las versiones alfa o beta. En muchos de estos casos, el objeto se puede convertir al tipo correcto.
La propiedad beta mapId de MapOptions causa el siguiente error.
error TS2345: Argument of type '{ center: google.maps.LatLng; zoom: number;
mapId: string; }' is not assignable to parameter of type 'MapOptions'. Object
literal may only specify known properties, and 'mapId' does not exist in type
'MapOptions'.
El error anterior se puede corregir con la siguiente conversión.
Algunas bibliotecas pueden usar un paquete que no sea @types/google.maps, lo que puede causar conflictos. Usa la opción del compilador skipLibCheck para evitar problemas de tipos inconsistentes.
{"compilerOptions":{"skipLibCheck":true}}
Especifica typeRoots
Algunos frameworks, como Angular, pueden requerir que se especifique la opción del compilador typeRoots para incluir tipos instalados desde @types/google.maps y todos los demás paquetes "@types".
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Falta la información que necesito","missingTheInformationINeed","thumb-down"],["Muy complicado o demasiados pasos","tooComplicatedTooManySteps","thumb-down"],["Desactualizado","outOfDate","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Problema con las muestras o los códigos","samplesCodeIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-08-27 (UTC)"],[[["\u003cp\u003eTypeScript can enhance Google Maps development by providing static typing and improved code maintainability.\u003c/p\u003e\n"],["\u003cp\u003eUse the \u003ccode\u003e@types/google.maps\u003c/code\u003e package from DefinitelyTyped for TypeScript support in your Google Maps projects.\u003c/p\u003e\n"],["\u003cp\u003eAlpha and beta Google Maps features may require type casting to avoid TypeScript errors.\u003c/p\u003e\n"],["\u003cp\u003eIn case of conflicting type definitions, consider utilizing the \u003ccode\u003eskipLibCheck\u003c/code\u003e compiler option to bypass type checking of external libraries.\u003c/p\u003e\n"],["\u003cp\u003eWhen necessary, configure \u003ccode\u003etypeRoots\u003c/code\u003e in your TypeScript configuration to ensure proper inclusion of Google Maps type definitions.\u003c/p\u003e\n"]]],[],null,["# TypeScript and Google Maps\n\n[TypeScript](https://www.typescriptlang.org/) is a typed superset of JavaScript\nthat compiles to plain JavaScript. The snippet below demonstrates simple usage\nof Google Maps using TypeScript. \n\n let map: google.maps.Map;\n const center: google.maps.LatLngLiteral = {lat: 30, lng: -110};\n\n function initMap(): void {\n map = new google.maps.Map(document.getElementById(\"map\") as HTMLElement, {\n center,\n zoom: 8\n });\n }\n\nGetting Started\n---------------\n\nThe\n[DefinitelyTyped project](https://github.com/DefinitelyTyped/DefinitelyTyped) is\nan open source projects that maintains type\n[declaration files](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)\nfor many packages including Google Maps. The Google Maps JavaScript declaration\nfiles (see\n[source files](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google.maps)\non GitHub) can be installed using NPM from the\n[@types/google.maps](https://www.npmjs.com/package/@types/google.maps) package. \n\n npm i -D @types/google.maps\n\n| **Note:** These types are automatically generated. To report an issue with these types, open a [support ticket](https://issuetracker.google.com/issues/new?component=188853&template=788207).\n\nAlpha and Beta Features\n-----------------------\n\nThe\n[types](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google.maps)\ntypically don't have the properties, functions, or classes found in alpha or\nbeta releases. In many of these cases, the object can be cast to the correct\ntype.\n\nThe following error is caused by the `mapId` beta property for `MapOptions`. \n\n```\nerror TS2345: Argument of type '{ center: google.maps.LatLng; zoom: number;\nmapId: string; }' is not assignable to parameter of type 'MapOptions'. Object\nliteral may only specify known properties, and 'mapId' does not exist in type\n'MapOptions'.\n```\n\nThe above error can be corrected with the cast below. \n\n { center: {lat: 30, lng: -110}, zoom: 8, mapId: '1234' } as google.maps.MapOptions\n\nConflicting @types packages\n---------------------------\n\nSome libraries may use a package other than\n[@types/google.maps](https://www.npmjs.com/package/@types/google.maps),\nwhich may cause conflicts. Use the\n[skipLibCheck](https://www.typescriptlang.org/tsconfig#skipLibCheck)\ncompiler option to avoid issues with inconsistent types. \n\n {\n \"compilerOptions\": {\n \"skipLibCheck\": true\n }\n }\n\nSpecify typeRoots\n-----------------\n\nSome frameworks such as Angular may require specifying the\n[typeRoots](https://www.typescriptlang.org/tsconfig#typeRoots)\ncompiler option to include types installed from\n[@types/google.maps](https://www.npmjs.com/package/@types/google.maps)\nand all other \"@types\" packages.\n**Note:** By default all visible \"@types\" packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible. \n\n {\n ...\n \"compilerOptions\": {\n ...\n \"typeRoots\": [\n \"node_modules/@types\",\n ],\n ...\n }\n }"]]