My Angular component relies on the google.maps.Map
class. Here's what it looks like:
export class MapViewComponent implements OnInit {
@Input()
public mapOptions: google.maps.MapOptions;
public map: google.maps.Map;
@ViewChild("map", { static: true })
mapDomNode: ElementRef;
public ngOnInit() {
this.map = new google.maps.Map(this.mapDomNode.nativeElement, this.mapOptions);
}
}
To install Google Maps type definitions, I followed the instructions in the documentation:
npm i -D @types/google.maps
Now, I'm trying to write unit tests for my component using an approach from a helpful SO answer (I use Karma for testing):
window['google'] = {
maps: {
Map: () => ({})
}
};
const spy = spyOn(window['google']['maps'], 'Maps').and.returnValue({});
I encounter the error:
Type '() => {}' is not assignable to type 'typeof Map'.
Type '() => {}' provides no match for the signature 'new (mapDiv: Element, opts?: MapOptions): Map'.ts(2322)
index.d.ts(3223, 9): The expected type comes from property 'Map' which is declared here on type 'typeof maps'
I've tried various techniques, like
Map: () => { return {} as google.maps.Map }
, but TypeScript keeps showing type errors.
How can I substitute any object for the google.maps.Map
type?