Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript:如何使用命名空间中的类或接口_Javascript_Typescript - Fatal编程技术网

Javascript Typescript:如何使用命名空间中的类或接口

Javascript Typescript:如何使用命名空间中的类或接口,javascript,typescript,Javascript,Typescript,我想扩展此文件中命名空间google.maps中定义的类: @类型/谷歌地图/索引d.ts 这是透明的,没有错误。但是在运行时,我在构造函数中得到一个错误,因为google.mapsglobal仍然没有定义 我做错了什么?例如,您需要先导入它 import Marker from 'googlemaps'; class MyMarker extends Marker 顺便说一句,我的退路是使用接口而不是类,并进行一系列TS类型转换练习。它可以工作,但不是最佳的 // new google.

我想扩展此文件中命名空间
google.maps
中定义的类:

@类型/谷歌地图/索引d.ts

这是透明的,没有错误。但是在运行时,我在构造函数中得到一个错误,因为
google.maps
global仍然没有定义


我做错了什么?

例如,您需要先导入它

import Marker from 'googlemaps';

class MyMarker extends Marker 

顺便说一句,我的退路是使用
接口
而不是
,并进行一系列TS类型转换练习。它可以工作,但不是最佳的

// new google.maps.Marker for MarkerClusterer
export interface UuidMarker extends Marker {
  uuid: string;
}

/**
 * Hack: "extend" google.maps.Marker to include uuid property
 *  guard with this.ready.then() to ensure google.maps is loaded
 */
function UuidMarkerFactory(uuid: string, options?: MarkerOptions) : Promise<UuidMarker> {
  return this.ready.then( ()=>{
    let gmOptions = options as any as google.maps.MarkerOptions;
    const marker = new google.maps.Marker( gmOptions );
    marker['uuid'] = uuid;
    return marker as any as UuidMarker;
  })
}
//MarkerClusterer的新google.maps.Marker
导出接口UuidMarker扩展标记{
uuid:字符串;
}
/**
*Hack:“扩展”google.maps.Marker以包含uuid属性
*使用此.ready.then()进行防护,以确保已加载google.maps
*/
函数UuidMarkerFactory(uuid:string,options?:MarkerOptions):承诺{
返回此。就绪。然后(()=>{
让gmOptions=选项与google.maps.MarkerOptions一样;
const marker=new google.maps.marker(gmOptions);
标记['uuid']=uuid;
返回标记作为UUIDMERKER;
})
}

你确定你添加了谷歌地图模块吗?我试过了,但是TS给我一个错误,
../@types/googlemaps/index.d.TS不是一个模块
。它是一个名称空间,实际的googlemaps库是动态加载的
// new google.maps.Marker for MarkerClusterer
export interface UuidMarker extends Marker {
  uuid: string;
}

/**
 * Hack: "extend" google.maps.Marker to include uuid property
 *  guard with this.ready.then() to ensure google.maps is loaded
 */
function UuidMarkerFactory(uuid: string, options?: MarkerOptions) : Promise<UuidMarker> {
  return this.ready.then( ()=>{
    let gmOptions = options as any as google.maps.MarkerOptions;
    const marker = new google.maps.Marker( gmOptions );
    marker['uuid'] = uuid;
    return marker as any as UuidMarker;
  })
}