从库默认导出创建类型

时间:2018-12-19 09:12:51

标签: typescript

我将OpenLayers与包含以下内容的定义文件一起使用:

// Type definitions for ol 4.6
// Project: https://github.com/openlayers/openlayers/tree/master/package#readme
// Definitions by: Yair Tawil <https://github.com/yairtawil>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as ol from 'openlayers';

export default ol;

我想用正确的类型(很明显)键入变量,直到现在我们仍在进行

import Map from 'ol/map';
...
olMap: Map;

现在,我正在尝试将其更改为

import * as OpenLayers from 'ol';
...
// want to do this
olMap: OpenLayers.Map;
// But can only do this
olMap: OpenLayers.default.Map;

是否可以将默认导出声明为要使用的类型?

1 个答案:

答案 0 :(得分:1)

如果这是默认导入,则使用默认导入语法,一切都会按预期进行:

import OpenLayers from 'ol';

您可以获取默认类型并像这样使用它(但我建议您使用第一个选项):

import * as OpenLayers from 'ol';
type ol = typeof OpenLayers.default
let olMap: ol.Map;