1

I want to extend a class defined in the namespace google.maps in this file:

@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.

What am I doing wrong?

1
  • did u sure that u added google map module? Commented Oct 15, 2016 at 11:12

2 Answers 2

1

You need to import it first, for example

import Marker from 'googlemaps';

class MyMarker extends Marker 
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that, but TS gives me an error, .../@types/googlemaps/index.d.ts is not a module. It is a namespace and the actual googlemaps lib is loaded dynamically
0

BTW, my fallback is to use an interface instead of class and do a bunch of TS typecast gymnastics. 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;
  })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.