你如何从`@types` typescript 2.0 导入类型定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39818429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you import type definitions from `@types` typescript 2.0
提问by michael
I'm using typescript 2.0 with the lastest [email protected]
build process.
我正在使用带有最新[email protected]
构建过程的typescript 2.0 。
I installed the google-maps types like this:
我安装了这样的谷歌地图类型:
npm install @types/google-maps --save-dev --save-exact
npm install @types/google-maps --save-dev --save-exact
and I'm trying to import some of the type definitions into my code like this
我正在尝试将一些类型定义导入到我的代码中
/// <reference types="google-maps" />
import { LatLng, LatLngBounds } from 'google-maps';
but I get this typescript error:
但我收到了这个打字稿错误:
./node_modules/@types/google-maps/index.d.ts has no exported member 'LatLng'
./node_modules/@types/google-maps/index.d.ts has no exported member 'LatLng'
and if I look in the source, I actually find the definition in
如果我查看源代码,我实际上会在其中找到定义
./node_modules/@types/google-maps/node_modules/@types/googlemaps/index.d.ts
./node_modules/@types/google-maps/node_modules/@types/googlemaps/index.d.ts
回答by Ropez
Add a reference to the types package in the file tsconfig.json
in the root folder of your project:
tsconfig.json
在项目根文件夹中的文件中添加对 types 包的引用:
"types": [
"google-maps"
]
Don't import anything in your source files, the types are globally defined.
不要在源文件中导入任何内容,类型是全局定义的。
回答by jifeng.yin
The import
only works for the current package, not its dependencies.
该import
只适用于当前的包,而不是它的依赖性。
So you need to import { LatLng, LatLngBounds } from 'googlemaps'
所以你需要 import { LatLng, LatLngBounds } from 'googlemaps'
回答by Sam
You are checking the wrong declaration file. The one you are using is: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-maps/index.d.tswhich does not expose LatLng.
您正在检查错误的声明文件。您正在使用的是:https: //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-maps/index.d.ts,它不公开 LatLng。
The declaration file you linked to is: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/googlemaps/index.d.ts
您链接到的声明文件是:https: //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/googlemaps/index.d.ts
googlemaps
vs google-maps
googlemaps
对比 google-maps