导入 Esri 类型时出错 "Cannot use 'new' with an expression whose type lacks a call or construct signature."

Error "Cannot use 'new' with an expression whose type lacks a call or construct signature." when importing Esri types

我觉得这里缺少一些非常简单的东西。我有一个正在构建的 class,它使用 Esri ArcGIS API,但是当我从 arcgis-js-apid.ts 文件导入类型定义时,我得到打字稿错误 "Cannot use 'new' with an expression whose type lacks a call or construct signature."

例如:

import * as IMap from 'esri/Map';

export class Foo {
    bar: (Map: IMap) {
        const map = new Map(); // <-- error here
    }
}

来自 d.ts 文件的相关片段:

declare namespace __esri {
    /* snip */
    interface Map extends Accessor, LayersMixin {
        allLayers: Collection;
        basemap: Basemap;
        ground: Ground;
    }

    interface MapConstructor {
        new(properties?: MapProperties): Map;
    }

    export const Map: MapConstructor;
    /* snip */
}

declare module "esri/Map" {
    import Map = __esri.Map;
    export = Map;
}

看起来类型定义对我来说是正确的,那么我做错了什么会让 Typescript 认为 IMap 类型没有构造函数?

您参数中的 MapIMap 类型的一个实例。如果您想将其键入为构造函数,请将其键入 typeof IMap:

bar (Map: typeof IMap) {
    const map = new Map(); 
}