【问题标题】:How do you import type definitions from `@types` typescript 2.0如何从`@types` typescript 2.0 导入类型定义
【发布时间】:2017-02-10 14:53:16
【问题描述】:

我正在使用带有最新 ionic@RC.0 构建过程的 typescript 2.0。

我安装了这样的谷歌地图类型:

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

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

/// <reference types="google-maps" />
import { LatLng, LatLngBounds } from 'google-maps';

但我收到此打字稿错误:

./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

【问题讨论】:

    标签: typescript


    【解决方案1】:

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

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

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • 其实我的回答和@Sam一样。请注意是googlemaps
    【解决方案2】:

    您正在检查错误的声明文件。您正在使用的是: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-maps/index.d.ts 不暴露 LatLng。

    您链接到的声明文件是:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/googlemaps/index.d.ts

    googlemapsgoogle-maps

    【讨论】:

      【解决方案3】:

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

      "types": [
          "google-maps"
      ]
      

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

      【讨论】:

      猜你喜欢
      • 2019-10-06
      • 2019-04-30
      • 2019-03-02
      • 1970-01-01
      • 2017-06-08
      • 2020-01-28
      • 2022-11-27
      • 2016-12-29
      • 2018-07-11
      相关资源
      最近更新 更多