如何安装谷歌地图的Typescript类型

Sim*_*n H 44 typescript

怎么做 - 我尝试过组合

typings install [googlemaps | google.maps] [--ambient] --save
Run Code Online (Sandbox Code Playgroud)

并最终得到此错误的变化

打字ERR!消息无法在注册表中找到"npm"的"googlemaps".

根据Amy的建议,我也下载到相关目录并添加

/// <reference path="main/ambient/google.maps/google.maps.d.ts" />
Run Code Online (Sandbox Code Playgroud)

到我的main.d.ts(一个明显被阅读的文件,因为我没有得到其他错误).

我在网上找不到任何回答这个问题的内容

我的最终目标是摆脱这种错误

错误TS2503:找不到命名空间'google'.

Ste*_*aul 79

因此,这个例外的原因是需要从应用程序包中单独下载地图脚本.这不是你典型的npm install地方,你得到你的.js.ts包装好的消费.

TLDR:可以通过npm安装打字,但必须通过<script>标签下载.js脚本(对于SPA,这个标签可以即时附加到您的网页,以改善您的应用程序的初始加载时间,这就是我做).

我推荐的路径如下:

安装

npm install --save @types/googlemaps
Run Code Online (Sandbox Code Playgroud)

进口

import {} from 'googlemaps';
Run Code Online (Sandbox Code Playgroud)

加载和使用(此功能确保地图脚本仅附加到页面一次,以便可以反复调用它)

addMapsScript() {
  if (!document.querySelectorAll(`[src="${googleMapsUrl}"]`).length) { 
    document.body.appendChild(Object.assign(
      document.createElement('script'), {
        type: 'text/javascript',
        src: googleMapsUrl,
        onload: () => doMapInitLogic()
      }));
  } else {
    this.doMapInitLogic();
  }
}
Run Code Online (Sandbox Code Playgroud)

请记住,必须将地图脚本附加到页面,并且必须先下载脚本,然后才能发生其他任何事情.例如,如果你正在使用Angular,我将addMapsScript()逻辑包装在一个observable中并放入我的地图组件路由解析器中.

使用类型(类型定义包括但不限于):

const mapRef: google.maps.Map;
const bounds: google.maps.LatLngBounds;
const latLng: google.maps.LatLng;
Run Code Online (Sandbox Code Playgroud)

摆脱警告:@types/googlemaps/index.d.ts' is not a module.

在您调用的项目根目录中添加一个文件index.d.ts并插入以下内容:

declare module 'googlemaps';
Run Code Online (Sandbox Code Playgroud)

更新01.06.2018(@DicBrus的调查结果):

为了导入谷歌命名空间并摆脱恼人的错误"无法找到命名空间'谷歌'"你应该确保你已经导入了命名空间定义 @types/googlemaps

有两种方法可以做到:
1.三重斜杠指令

/// <reference path="<relevant path>/node_modules/@types/googlemaps/index.d.ts" />
Run Code Online (Sandbox Code Playgroud)

工作得很好,但不那么优雅.

文档可以在这里找到:https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

  1. 确保它已导入tsconfig.json,因为它是导入的,您不需要另外导入

详细手册如下:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

你应该检查以下两个小节compilerOptions: typeRootstypes

默认情况下,除非您准确指定了所需的类型定义,否则将导入node_modules/@ types下的所有类型定义.

在我的特定情况下,我有以下部分:

"types": []
Run Code Online (Sandbox Code Playgroud)

它禁用自动包含@types包.

删除此行重新解决了我的问题,并添加了三重斜杠指令帮助.但我选择了第二种解决方案.

至于"空"导入,我没有找到任何解释如何以及为什么它起作用.我想,它导入任何模块或类,但它进口的命名空间.此解决方案不适合我,因为IDE将此导入标记为"未使用",并且可以轻松删除.例如,webstorm的命令Ctrl + Alt + O - 美化代码并删除所有不必要的导入.

  • 2021:我必须将“google.maps”添加到“types”,而不是“googlemaps” (5认同)
  • doMapInitLogic()是一个可以创建的函数(您可以随意重命名).你来对了吗? (2认同)
  • 是的,它可以是 Javascript,但是我想知道您为什么要查看有关 Typescript 的这个问题?;) 如果您只使用 Javascript,我建议您首先查看 Google 的官方文档 https://developers.google.com/maps/documentation/javascript/tutorial (2认同)
  • 因此,您实际上不需要使用Typescript来使用谷歌地图,在使用地图API时使用类型定义会很不错.现在,如果您尝试声明一个新的google.maps.Map,您仍会收到打字稿错误 - 类似于"未定义谷歌".你可以做些什么来修复它,在你的源文件的顶部(你的课外)写'declare const google'. (2认同)
  • 嘿,我想我找到了一个更干净的版本:`/// &lt;reference types =“ googlemaps” /&gt;`:解析这些包名称的过程类似于在import语句中解析模块名的过程。考虑三重斜杠引用类型指令的一种简单方法是将其导入声明包。https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html这消除了丑陋的相对路径的需要! (2认同)

Sim*_*n H 19

在实践中,我的用例是Angular CLI,我需要的只是

npm install --save @types/google-maps
Run Code Online (Sandbox Code Playgroud)


use*_*006 14

我在离子2项目上测试了这些步骤,它完美地运行:

  1. 全球安装打字:

npm install typings --global
Run Code Online (Sandbox Code Playgroud)
  1. 通过打字安装google.maps

typings install dt~google.maps --global --save
Run Code Online (Sandbox Code Playgroud)
  1. 打开tsconfig.json并将" typings /*.d.ts"添加到"include"数组中,如下所示(tsconfig.json).

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts",
    "typings/*.d.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*red 11

typings install google.maps --global

您需要--global(以前是--ambient)标志来搜索DefinitlyTyped

  • 打字从[v1.0.0](https://github.com/typings/core/releases/tag/v1.0.0)开始将"环境"改为"全球"."typings install google.maps --global"或者`typings install dt~google.maps --global` (7认同)

Mat*_*yas 10

截至目前,正确的安装方式是:

typings install dt~google.maps --global [--save]
Run Code Online (Sandbox Code Playgroud)


Joh*_*srk 8

至于打字稿2

npm install --save @types/googlemaps
Run Code Online (Sandbox Code Playgroud)

将typeroots添加到您的tsconfig

{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types/"]
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么这个 `typeRoots` 是必要的?那应该是默认的。 (2认同)

小智 6

我的解决方案(适用于Vue2.x):

安装

npm install --save @types/googlemaps
Run Code Online (Sandbox Code Playgroud)

将脚本添加到index.html

npm install --save @types/googlemaps
Run Code Online (Sandbox Code Playgroud)

在根文件夹类型/index.d.ts中创建

将以下几行放在这里:

<script src="https://maps.googleapis.com/maps/api/js?key=XXX&libraries=YYYY"></script>
Run Code Online (Sandbox Code Playgroud)

打开tsconfig.json并将“ types / *。d.ts”添加到“ include”数组中。

/// <reference path="../node_modules/@types/googlemaps/index.d.ts" />
declare module 'googlemaps';
Run Code Online (Sandbox Code Playgroud)


Tim*_*thy 5

斯蒂芬保罗清楚地解释了一切,但有一些重要的事情要提。tsconfigs 可以相互扩展。并且扩展一个可以覆盖父一个。就我而言,我在 app 目录下有另一个 tsconfig.app.json

types: []
Run Code Online (Sandbox Code Playgroud)

数组。正如斯蒂芬已经解释过的,这个空数组覆盖了 typeRoots。因此,只需删除所有相关 tsconfig 文件中的所有类型数组并确保

"typeRoots": ["node_modules/@types"]
Run Code Online (Sandbox Code Playgroud)

存在。不用说必须安装@types@googlemaps


小智 5

最简单的方法是使用三斜线指令。幸运的是,有一种替代语法可以利用标准包解析:

 /// <reference types="googlemaps" />
Run Code Online (Sandbox Code Playgroud)

  • 这是在 VS Code 上唯一对我有用的解决方案。对我来说,我没有更改“tsconfig”,也没有添加任何“d.ts”文件。 (2认同)

fel*_*osh 5

我努力在窗口上定义google对象,最后通过扩展Window接口找到了一个好方法。

只需使用以下google-maps.d.ts命令创建一个文件:

import '@types/googlemaps';

declare global {
  interface Window {
    google: typeof google;
  }
}
Run Code Online (Sandbox Code Playgroud)

并把它添加到一个名为目录types在你的文件夹。然后指向文件中的该文件夹tsconfig.json

// tsconfig.json
compilerOptions: {
   ...
   "typeRoots": [
     "node_modules/@types",
     "types"
   ],
   ...
}
Run Code Online (Sandbox Code Playgroud)