错误“不能对类型缺少调用或构造签名的表达式使用'new'."导入Esri类型时 [英] Error "Cannot use 'new' with an expression whose type lacks a call or construct signature." when importing Esri types
问题描述
我觉得这里确实缺少一些简单的东西.我正在构建一个使用Esri ArcGIS API的类,但是当我从arcgis-js-api
的d.ts
文件导入类型定义时,出现打字稿错误无法在表达式中使用'new'其类型缺少调用或构造签名."
I feel like there is something really simple that I am missing here. I've got a class that I am building out that uses Esri ArcGIS API, but when I import the type definitions from the arcgis-js-api
's d.ts
file, I get the typescript error "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
类型没有构造函数?
Looks like the type definition is correct to me, so what am I doing wrong that would make Typescript think that the IMap
type doesn't have a constructor?
推荐答案
参数中的Map
是IMap
类型的实例.如果您想将其键入为构造函数,则将其键入为typeof IMap
:
The Map
in your parameter is an instance of the IMap
type. If you wanted to type it as the constructor instead, type it as typeof IMap
:
bar (Map: typeof IMap) {
const map = new Map();
}
这篇关于错误“不能对类型缺少调用或构造签名的表达式使用'new'."导入Esri类型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!