Skip to content

Commit dfe92fc

Browse files
authored
refactor(google-maps): use assert functions to avoid casting to non-nullable value (#19178)
We can take advantage of TypeScript's `asserts` keyword so that we don't have to cast values to be non-nullable, when we've already asserted that they exist.
1 parent 27a812b commit dfe92fc

File tree

8 files changed

+83
-85
lines changed

8 files changed

+83
-85
lines changed

src/google-maps/google-map/google-map.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
290290
bounds: google.maps.LatLngBounds|google.maps.LatLngBoundsLiteral,
291291
padding?: number|google.maps.Padding) {
292292
this._assertInitialized();
293-
this.googleMap!.fitBounds(bounds, padding);
293+
this.googleMap.fitBounds(bounds, padding);
294294
}
295295

296296
/**
@@ -299,7 +299,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
299299
*/
300300
panBy(x: number, y: number) {
301301
this._assertInitialized();
302-
this.googleMap!.panBy(x, y);
302+
this.googleMap.panBy(x, y);
303303
}
304304

305305
/**
@@ -308,7 +308,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
308308
*/
309309
panTo(latLng: google.maps.LatLng|google.maps.LatLngLiteral) {
310310
this._assertInitialized();
311-
this.googleMap!.panTo(latLng);
311+
this.googleMap.panTo(latLng);
312312
}
313313

314314
/**
@@ -319,7 +319,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
319319
latLngBounds: google.maps.LatLngBounds|google.maps.LatLngBoundsLiteral,
320320
padding?: number|google.maps.Padding) {
321321
this._assertInitialized();
322-
this.googleMap!.panToBounds(latLngBounds, padding);
322+
this.googleMap.panToBounds(latLngBounds, padding);
323323
}
324324

325325
/**
@@ -328,7 +328,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
328328
*/
329329
getBounds(): google.maps.LatLngBounds|null {
330330
this._assertInitialized();
331-
return this.googleMap!.getBounds() || null;
331+
return this.googleMap.getBounds() || null;
332332
}
333333

334334
/**
@@ -337,7 +337,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
337337
*/
338338
getCenter(): google.maps.LatLng {
339339
this._assertInitialized();
340-
return this.googleMap!.getCenter();
340+
return this.googleMap.getCenter();
341341
}
342342

343343
/**
@@ -346,7 +346,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
346346
*/
347347
getClickableIcons(): boolean {
348348
this._assertInitialized();
349-
return this.googleMap!.getClickableIcons();
349+
return this.googleMap.getClickableIcons();
350350
}
351351

352352
/**
@@ -355,7 +355,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
355355
*/
356356
getHeading(): number {
357357
this._assertInitialized();
358-
return this.googleMap!.getHeading();
358+
return this.googleMap.getHeading();
359359
}
360360

361361
/**
@@ -364,7 +364,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
364364
*/
365365
getMapTypeId(): google.maps.MapTypeId|string {
366366
this._assertInitialized();
367-
return this.googleMap!.getMapTypeId();
367+
return this.googleMap.getMapTypeId();
368368
}
369369

370370
/**
@@ -373,7 +373,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
373373
*/
374374
getProjection(): google.maps.Projection|null {
375375
this._assertInitialized();
376-
return this.googleMap!.getProjection();
376+
return this.googleMap.getProjection();
377377
}
378378

379379
/**
@@ -382,7 +382,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
382382
*/
383383
getStreetView(): google.maps.StreetViewPanorama {
384384
this._assertInitialized();
385-
return this.googleMap!.getStreetView();
385+
return this.googleMap.getStreetView();
386386
}
387387

388388
/**
@@ -391,7 +391,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
391391
*/
392392
getTilt(): number {
393393
this._assertInitialized();
394-
return this.googleMap!.getTilt();
394+
return this.googleMap.getTilt();
395395
}
396396

397397
/**
@@ -400,7 +400,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
400400
*/
401401
getZoom(): number {
402402
this._assertInitialized();
403-
return this.googleMap!.getZoom();
403+
return this.googleMap.getZoom();
404404
}
405405

406406
/**
@@ -409,7 +409,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
409409
*/
410410
get controls(): Array<google.maps.MVCArray<Node>> {
411411
this._assertInitialized();
412-
return this.googleMap!.controls;
412+
return this.googleMap.controls;
413413
}
414414

415415
/**
@@ -418,7 +418,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
418418
*/
419419
get data(): google.maps.Data {
420420
this._assertInitialized();
421-
return this.googleMap!.data;
421+
return this.googleMap.data;
422422
}
423423

424424
/**
@@ -427,7 +427,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
427427
*/
428428
get mapTypes(): google.maps.MapTypeRegistry {
429429
this._assertInitialized();
430-
return this.googleMap!.mapTypes;
430+
return this.googleMap.mapTypes;
431431
}
432432

433433
/**
@@ -436,7 +436,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
436436
*/
437437
get overlayMapTypes(): google.maps.MVCArray<google.maps.MapType> {
438438
this._assertInitialized();
439-
return this.googleMap!.overlayMapTypes;
439+
return this.googleMap.overlayMapTypes;
440440
}
441441

442442
private _setSize() {
@@ -503,7 +503,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
503503
}
504504

505505
/** Asserts that the map has been initialized. */
506-
private _assertInitialized() {
506+
private _assertInitialized(): asserts this is {googleMap: google.maps.Map} {
507507
if (!this.googleMap) {
508508
throw Error('Cannot access Google Map information before the API has been initialized. ' +
509509
'Please wait for the API to load before trying to interact with it.');

src/google-maps/map-circle/map-circle.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class MapCircle implements OnInit, OnDestroy {
168168
this.circle = new google.maps.Circle(options);
169169
});
170170
this._assertInitialized();
171-
this.circle!.setMap(this._map.googleMap!);
171+
this.circle.setMap(this._map.googleMap!);
172172
this._eventManager.setTarget(this.circle);
173173
});
174174

@@ -193,7 +193,7 @@ export class MapCircle implements OnInit, OnDestroy {
193193
*/
194194
getBounds(): google.maps.LatLngBounds {
195195
this._assertInitialized();
196-
return this.circle!.getBounds();
196+
return this.circle.getBounds();
197197
}
198198

199199
/**
@@ -202,7 +202,7 @@ export class MapCircle implements OnInit, OnDestroy {
202202
*/
203203
getCenter(): google.maps.LatLng {
204204
this._assertInitialized();
205-
return this.circle!.getCenter();
205+
return this.circle.getCenter();
206206
}
207207

208208
/**
@@ -211,7 +211,7 @@ export class MapCircle implements OnInit, OnDestroy {
211211
*/
212212
getDraggable(): boolean {
213213
this._assertInitialized();
214-
return this.circle!.getDraggable();
214+
return this.circle.getDraggable();
215215
}
216216

217217
/**
@@ -220,7 +220,7 @@ export class MapCircle implements OnInit, OnDestroy {
220220
*/
221221
getEditable(): boolean {
222222
this._assertInitialized();
223-
return this.circle!.getEditable();
223+
return this.circle.getEditable();
224224
}
225225

226226
/**
@@ -229,7 +229,7 @@ export class MapCircle implements OnInit, OnDestroy {
229229
*/
230230
getRadius(): number {
231231
this._assertInitialized();
232-
return this.circle!.getRadius();
232+
return this.circle.getRadius();
233233
}
234234

235235
/**
@@ -238,7 +238,7 @@ export class MapCircle implements OnInit, OnDestroy {
238238
*/
239239
getVisible(): boolean {
240240
this._assertInitialized();
241-
return this.circle!.getVisible();
241+
return this.circle.getVisible();
242242
}
243243

244244
private _combineOptions(): Observable<google.maps.CircleOptions> {
@@ -256,15 +256,15 @@ export class MapCircle implements OnInit, OnDestroy {
256256
private _watchForOptionsChanges() {
257257
this._options.pipe(takeUntil(this._destroyed)).subscribe(options => {
258258
this._assertInitialized();
259-
this.circle!.setOptions(options);
259+
this.circle.setOptions(options);
260260
});
261261
}
262262

263263
private _watchForCenterChanges() {
264264
this._center.pipe(takeUntil(this._destroyed)).subscribe(center => {
265265
if (center) {
266266
this._assertInitialized();
267-
this.circle!.setCenter(center);
267+
this.circle.setCenter(center);
268268
}
269269
});
270270
}
@@ -273,12 +273,12 @@ export class MapCircle implements OnInit, OnDestroy {
273273
this._radius.pipe(takeUntil(this._destroyed)).subscribe(radius => {
274274
if (radius !== undefined) {
275275
this._assertInitialized();
276-
this.circle!.setRadius(radius);
276+
this.circle.setRadius(radius);
277277
}
278278
});
279279
}
280280

281-
private _assertInitialized() {
281+
private _assertInitialized(): asserts this is {circle: google.maps.Circle} {
282282
if (!this._map.googleMap) {
283283
throw Error(
284284
'Cannot access Google Map information before the API has been initialized. ' +

src/google-maps/map-ground-overlay/map-ground-overlay.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
8484
this.groundOverlay = new google.maps.GroundOverlay(this.url, this.bounds, options);
8585
});
8686
this._assertInitialized();
87-
this.groundOverlay!.setMap(this._map.googleMap!);
87+
this.groundOverlay.setMap(this._map.googleMap!);
8888
this._eventManager.setTarget(this.groundOverlay);
8989
});
9090

@@ -108,7 +108,7 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
108108
*/
109109
getBounds(): google.maps.LatLngBounds {
110110
this._assertInitialized();
111-
return this.groundOverlay!.getBounds();
111+
return this.groundOverlay.getBounds();
112112
}
113113

114114
/**
@@ -118,7 +118,7 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
118118
*/
119119
getOpacity(): number {
120120
this._assertInitialized();
121-
return this.groundOverlay!.getOpacity();
121+
return this.groundOverlay.getOpacity();
122122
}
123123

124124
/**
@@ -128,7 +128,7 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
128128
*/
129129
getUrl(): string {
130130
this._assertInitialized();
131-
return this.groundOverlay!.getUrl();
131+
return this.groundOverlay.getUrl();
132132
}
133133

134134
private _combineOptions(): Observable<google.maps.GroundOverlayOptions> {
@@ -145,12 +145,12 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
145145
this._opacity.pipe(takeUntil(this._destroyed)).subscribe(opacity => {
146146
if (opacity) {
147147
this._assertInitialized();
148-
this.groundOverlay!.setOpacity(opacity);
148+
this.groundOverlay.setOpacity(opacity);
149149
}
150150
});
151151
}
152152

153-
private _assertInitialized() {
153+
private _assertInitialized(): asserts this is {groundOverlay: google.maps.GroundOverlay} {
154154
if (!this._map.googleMap) {
155155
throw Error(
156156
'Cannot access Google Map information before the API has been initialized. ' +

src/google-maps/map-info-window/map-info-window.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
135135
*/
136136
close() {
137137
this._assertInitialized();
138-
this.infoWindow!.close();
138+
this.infoWindow.close();
139139
}
140140

141141
/**
@@ -144,7 +144,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
144144
*/
145145
getContent(): string|Node {
146146
this._assertInitialized();
147-
return this.infoWindow!.getContent();
147+
return this.infoWindow.getContent();
148148
}
149149

150150
/**
@@ -154,7 +154,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
154154
*/
155155
getPosition(): google.maps.LatLng|null {
156156
this._assertInitialized();
157-
return this.infoWindow!.getPosition();
157+
return this.infoWindow.getPosition();
158158
}
159159

160160
/**
@@ -163,7 +163,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
163163
*/
164164
getZIndex(): number {
165165
this._assertInitialized();
166-
return this.infoWindow!.getZIndex();
166+
return this.infoWindow.getZIndex();
167167
}
168168

169169
/**
@@ -174,7 +174,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
174174
this._assertInitialized();
175175
const marker = anchor ? anchor.marker : undefined;
176176
this._elementRef.nativeElement.style.display = '';
177-
this.infoWindow!.open(this._googleMap.googleMap, marker);
177+
this.infoWindow.open(this._googleMap.googleMap, marker);
178178
}
179179

180180
private _combineOptions(): Observable<google.maps.InfoWindowOptions> {
@@ -191,20 +191,20 @@ export class MapInfoWindow implements OnInit, OnDestroy {
191191
private _watchForOptionsChanges() {
192192
this._options.pipe(takeUntil(this._destroy)).subscribe(options => {
193193
this._assertInitialized();
194-
this.infoWindow!.setOptions(options);
194+
this.infoWindow.setOptions(options);
195195
});
196196
}
197197

198198
private _watchForPositionChanges() {
199199
this._position.pipe(takeUntil(this._destroy)).subscribe(position => {
200200
if (position) {
201201
this._assertInitialized();
202-
this.infoWindow!.setPosition(position);
202+
this.infoWindow.setPosition(position);
203203
}
204204
});
205205
}
206206

207-
private _assertInitialized() {
207+
private _assertInitialized(): asserts this is {infoWindow: google.maps.InfoWindow} {
208208
if (!this._googleMap.googleMap) {
209209
throw Error(
210210
'Cannot access Google Map information before the API has been initialized. ' +

0 commit comments

Comments
 (0)