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

时间:2016-10-15 11:05:01

标签: javascript typescript

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

  

@类型/谷歌地图/ index.d.ts

class MyMarker extends google.maps.Marker {
  constructor(options: google.maps.MarkerOptions){
    super(options);
  }
}

这种情况没有错误。但是在运行时,我在构造函数中出现错误,因为google.maps全局仍未定义。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您需要先导入它,例如

HELO

答案 1 :(得分:0)

顺便说一句,我的后备是使用interface代替class并做一堆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;
  })
}