googlemaps / googlemaps/google-maps-services-js
Exported `Place` type has fully optional properties
- Dominant language
- TypeScript
- Stars
- 3.1k
- Forks
- 654
- Avg merge
- 3m
- Merged PRs (30d)
- 4
Description
Every single property belonging to `Place` is optional when that isn't practical in reality and puts the developer in a really awkward position when things that should _**always**_ be present can be `undefined`.
#### Environment details
1. `client.placeDetails` > `PlaceDetailsResponse` > `PlaceDetailsResponseData` > `Place`
2. macOS 12.1 (21C52)
3. `"@googlemaps/google-maps-services-js": "^3.3.6",`, `node v16.13.0`
#### Steps to reproduce
1. Use `client.placeDetails` within a typescript app
2. Examine the type of the saved response's `Place` within `data.result`
OR
See [this exported type](https://github.com/googlemaps/google-maps-services-js/blob/66720cb38652e42e31588a4132cdabf04b91355f/src/common.ts#L396) that marks all properties as optional...
#### Code example
```typescript
interface SimplePlaceDetails {
address: string;
latitude: number;
longitude: number;
rating?: number;
numReviews?: number;
}
// ...
const client = new Client({});
const response = await client.placeDetails({
params: {
place_id: placeId,
sessiontoken: sessionToken,
key: env.googleApiKey,
},
});
const { result } = response.data;
const { lat: latitude, lng: longitude } = result.geometry?.location ?? {};
return {
address: result.formatted_address,
latitude,
longitude,
rating: result.rating,
numReviews: result.reviews?.length,
};
```
Compilation error (abbreviated): `Type '{ address: string | undefined; latitude: number | undefined; longitude: number | undefined; rating: number | undefined; numReviews: number | undefined; }' is not assignable to type 'SimplePlaceDetails'.`
Literally the entirety of `Place` is a partial so every single one of its properties can be undefined`. I get this for certain values like `rating` and `reviews` but it makes zero sense for things like `formatted_address`, `lat`, `lng`, etc. unless the docs are updated to provide context for when they may actually be undefined.
I need to get lat and long for my app to function properly and right now I'm going to have to make an assumption that it's defined and just throw an error otherwise, which kinda sucks...
Can someone take a pass at cleaning that type up and making sure that only legitimately optional params are marked as being optionals? I'm totally onboard with logically optional things being optional, but either devs need good docs to understand when things may be undefined or the type needs to be cleaned up a bit.
Thank you!!!
Contributor guide
Assessment
This issue has not been assessed yet.