如何从`.d.ts` 文件导入?

mic*_*ael 5 typescript angular

这条线在一个使用 angular2 RC4 的项目中工作

import * as mapTypes from '../../../../node_modules/angular2-google-maps/core/services/google-maps-types.d.ts';

发生了什么?

现在我正在尝试使用 RC6 的新种子文件,同一行给了我这个错误

error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing '../../../../node_modules/angular2-google-maps/core/services/google-maps-types' instead.

但是,如果我进行建议的更改,我会得到

Cannot find module '../../../../node_modules/angular2-google-maps/core/services/google-maps-types'

.d.ts文件如下所示:

/**
 * angular2-google-maps - Angular 2 components for Google Maps
 * @version v0.12.0
 * @link https://github.com/SebastianM/angular2-google-maps#readme
 * @license MIT
 */
export declare var google: any;
export interface GoogleMap {
    constructor(el: HTMLElement, opts?: MapOptions): void;
    panTo(latLng: LatLng | LatLngLiteral): void;
    setZoom(zoom: number): void;
    addListener(eventName: string, fn: Function): void;
    getCenter(): LatLng;
    setCenter(latLng: LatLng | LatLngLiteral): void;
    getBounds(): LatLngBounds;
    getZoom(): number;
    setOptions(options: MapOptions): void;
}
...
Run Code Online (Sandbox Code Playgroud)

并且匹配的.ts文件看起来像

/**
 * angular2-google-maps - Angular 2 components for Google Maps
 * @version v0.12.0
 * @link https://github.com/SebastianM/angular2-google-maps#readme
 * @license MIT
 */
"use strict";

//# sourceMappingURL=google-maps-types.js.map
Run Code Online (Sandbox Code Playgroud)

注意:这个包有一个与 RC6 匹配的更新版本,但我应该能够得到它至少在 TS 中编译

kub*_*ube 3

如果您使用的是最新版本的 TypeScript 并且类型定义嵌入在您的 npm 包中,那么您只需从模块导入即可:

import { GoogleMap } from 'angular2-google-maps/core/services/google-maps-types'
Run Code Online (Sandbox Code Playgroud)

即使它是一个接口。

事实上,您正在从 导入定义core/services/googles-maps-types.js,但该文件的存在只是为了提供与其.d.ts文件关联的定义。

它的工作方式就像直接从.ts文件导入一样,但当您在 ​​npm 模块中时,它们会嵌入定义文件以允许定义导入。