如何从`@ types` typescript 2.0导入类型定义

mic*_*ael 17 typescript

我正在使用最近的ionic@RC.0构建过程的typescript 2.0 .

我安装了google-maps类型,如下所示:

npm install @types/google-maps --save-dev --save-exact

我正在尝试将一些类型定义导入到我的代码中

/// <reference types="google-maps" />
import { LatLng, LatLngBounds } from 'google-maps';
Run Code Online (Sandbox Code Playgroud)

但我得到这个打字稿错误:

./node_modules/@types/google-maps/index.d.ts has no exported member 'LatLng'

如果我查看源代码,我实际上找到了定义

./node_modules/@types/google-maps/node_modules/@types/googlemaps/index.d.ts

Rop*_*pez 10

tsconfig.json在项目根文件夹中的文件中添加对 types 包的引用:

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

不要在源文件中导入任何内容,类型是全局定义的。

  • 问题是,如果你开始手动添加一些类型,你将必须像这样指定所有类型,据我所知 TypeScript 不会自动加载它们。此功能首先是白名单,而不是全局导入系统。 (2认同)

jif*_*yin 2

import适用于当前包,不适用于其依赖项。

所以你需要import { LatLng, LatLngBounds } from 'googlemaps'

  • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。- [来自评论](/review/low-quality-posts/18095085) (4认同)