繁体   English   中英

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

[英]How do I import from a `.d.ts` file?

此行适用于使用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;
}
...

和匹配的.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

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

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

import { GoogleMap } from 'angular2-google-maps/core/services/google-maps-types'

即使它是一个界面。

实际上,您从core/services/googles-maps-types.js导入定义,但此文件仅用于提供其.d.ts文件中关联的定义。

它就像你直接从.ts文件导入一样工作,但是当你在npm模块中时,它们将定义文件嵌入到允许定义导入中。


通常在TypeScript中,您可以在.ts文件的顶部添加对定义文件的引用,如下所示:

/// <reference path="../../../../node_modules/angular2-google-maps/core/services/google-maps-types.d.ts"

之后,您可以导入定义文件中的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM