[英]Typescript: How do I use a class or interface from a namespace
I want to extend a class defined in the namespace google.maps
in this file:我想在这个文件中扩展命名空间google.maps
中定义的类:
@types/googlemaps/index.d.ts @types/googlemaps/index.d.ts
class MyMarker extends google.maps.Marker {
constructor(options: google.maps.MarkerOptions){
super(options);
}
}
This transpiles without errors.这可以无误地转换。 But on runtime, I get an error in the constructor because the google.maps
global is still undefined.但是在运行时,我在构造函数中遇到错误,因为google.maps
全局仍然未定义。
What am I doing wrong?我究竟做错了什么?
You need to import it first, for example您需要先导入它,例如
import Marker from 'googlemaps';
class MyMarker extends Marker
BTW, my fallback is to use an interface
instead of class
and do a bunch of TS typecast gymnastics.顺便说一句,我的回退是使用interface
而不是class
并做一堆 TS 类型转换体操。 It works but it's not optimal...它有效,但不是最佳...
// 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;
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.