Skip to content

Commit ec3f999

Browse files
mcreinhardcopybara-github
authored andcommitted
fix: make isPlaceResult() type predicate more robust to JS API changes
PiperOrigin-RevId: 925638490
1 parent 9d1fe0b commit ec3f999

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/utils/place_utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import {isOpen} from './opening_hours.js';
2121
export function isPlaceResult(place: Place|PlaceResult): place is PlaceResult {
2222
// To avoid depending on loading the API at runtime, we do not use
2323
// `instanceof google.map.places.Place`.
24-
// TODO: fix for property renaming safety?
25-
return !place.hasOwnProperty('id');
24+
return (place as Place).id === undefined;
2625
}
2726

2827
/**

src/utils/place_utils_test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {Environment} from '../testing/environment.js';
1212
import {FakeLatLng} from '../testing/fake_lat_lng.js';
1313
import {makeFakePlace, SAMPLE_FAKE_PLACE, SAMPLE_FAKE_PLACE_RESULT} from '../testing/fake_place.js';
1414

15-
import type {PlaceResult} from './googlemaps_types.js';
15+
import type {Place, PlaceResult} from './googlemaps_types.js';
1616
import {isPlaceResult, makePlaceFromPlaceResult, makeWaypoint, numericToPriceLevel, priceLevelToNumeric, renderAttribution} from './place_utils.js';
1717

1818
type PriceLevel = google.maps.places.PriceLevel;
@@ -31,6 +31,18 @@ describe('isPlaceResult', () => {
3131
const fakePlace = makeFakePlace({id: 'some_id', displayName: 'Name'});
3232
expect(isPlaceResult(fakePlace)).toBe(false);
3333
});
34+
35+
it('says that a Place with an id getter is not a PlaceResult', () => {
36+
class FakePlace {
37+
get id() {
38+
return 'some_id';
39+
}
40+
get displayName() {
41+
return 'Name';
42+
}
43+
}
44+
expect(isPlaceResult(new FakePlace() as Place)).toBe(false);
45+
});
3446
});
3547

3648
describe('priceLevelToNumeric', () => {

0 commit comments

Comments
 (0)