简体   繁体   English

如何导入其顶级元素是未导出的命名空间的Typescript类型定义文件?

[英]How do you import a Typescript type definition file whose top level element is a non-exported namespace?

I'm trying to use the @types/googlemaps type definition file. 我正在尝试使用@types/googlemaps类型定义文件。

The code looks like 代码看起来像

declare namespace google.maps {
    /***** Map *****/
    export class Map extends MVCObject {
        constructor(mapDiv: Element|null, opts?: MapOptions);
        fitBounds(bounds: LatLngBounds|LatLngBoundsLiteral): void;
        ...
        ...
        overlayMapTypes: MVCArray<MapType>;
    }

    export interface MapOptions {
        backgroundColor?: string;
        disableDoubleClickZoom?: boolean;
        draggable?: boolean;
        ...

When I try to use this type definition in my project, like so 当我尝试在项目中使用此类型定义时,就像这样

import * as google from 'googlemaps';

I get a compile error saying 我收到一个编译错误提示

Error:(2, 25) TS2306:File 'C:/Users/CodyB/musicappproj/src/node_modules/@types/googlemaps/index.d.ts' is not a module.

Why does it not consider this types definition file to be a module? 为什么不将这个类型定义文件视为模块? Am I using it wrong ? 我使用错了吗? Is the definition file wrong? 定义文件是否错误?

Why does it not consider this types definition file to be a module? 为什么不将这个类型定义文件视为模块?

The compiler does not consider this type definition file to be an ECMA6 module because the file lacks a top-level export/import. 编译器不认为此类型定义文件为ECMA6模块,因为该文件缺少顶级导出/导入。 Instead, the file nests/hides its exports inside a namespace and does not export the namespace. 而是,文件将其导出嵌套/隐藏在namespace ,而不导出名称空间。 As a result, the namespace becomes part of the global scope, and the file does not become a module. 结果,名称空间成为全局范围的一部分,并且文件不成为模块。

A file is a module only if it has a top-level import or export. 仅当文件具有顶级导入或导出时,它才是模块。 In the following code, both foo.ts and bar.ts are modules, but baz.ts is not, because it lacks a top-level import or export statement. 在以下代码中, foo.tsbar.ts都是模块,而baz.ts则不是,因为它缺少顶级的importexport语句。

// foo.ts
console.log('foo');
export {}

// bar.ts
import * as foo from './foo';
console.log('bar');

// baz.ts
console.log('baz');

// index.ts
import * as foo from './foo';
import * as bar from './bar';
import * as baz from './baz'; // File 'c:/dev/temp/baz.ts' is not a module.ts(2306)

Am I using it wrong? 我使用错了吗?

Yes. 是。 You are treating the file like it is a module - which it is not. 您正在像对待文件一样对待文件-事实并非如此。 The alternative is to access its members through the global namespace it defines. 另一种方法是通过其定义的全局名称空间访问其成员。 Here are two approaches: 这是两种方法:

One is to 'dot into' the namespace like this: 一种是像这样“点进”命名空间:

const div1 = new HTMLDivElement();
const map1 = new google.maps.Map(div1);

Another is to use destructuring like this: 另一个方法是使用如下结构:

const { Map } = google.maps;
const div2 = new HTMLDivElement();
const map2 = new Map(div2);

Is the definition file wrong? 定义文件是否错误?

No. It is taking a valid approach albeit one that is arguably more conventional for a browser (eg Firefox) application than it is for a NodeJS application. 不会。尽管采取了一种有效的方法,但可以说对于浏览器(例如Firefox)应用程序而言,它比对NodeJS应用程序更常规。

See also: 也可以看看:

https://www.typescriptlang.org/docs/handbook/namespaces.html https://www.typescriptlang.org/docs/handbook/namespaces.html

https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html

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

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