DirectionRequest departure_time can be 'now'
goleary opened this issue · comments
I've been using the google maps node sdk without issue passing 'now'
in the DirectionRequest to the direction service. I'm converting my code to TS and as such installed this typings module.
I have an error where I am passing 'now'
as the departure_time
even though it is allowed by the API (my code was working fine previously). I modifying the .d.ts file, like this departure_time?: Date | number | 'now';
and am going to use "now" as "now"
to cast it to the correct type. Open to alternate solutions as well.
Hi @goleary, thanks for proposing this.
I see no reference of such a usage in the official documentation, neither in the property validation method: departure_time
should be a timeStamp
.
Did you try to pass other strings like "yesterday" or "tonight" to see if the result is different?
Thanks,
Indri
you can see it documented here that now
is an acceptable value for this field
Alternatively, you can specify a value of
now
, which sets the departure time to the current time (correct to the nearest second).
Oh ok, didn’t notice that. Thanks
Can you please also propose the same PR to the DefinitelyTyped repository?
Sure, but before I do that @indrimuska would you confirm you think this is the best way to handle this scenario? It's a little weird to me that I have to use "now" as "now"
to cast the type so that I don't have any complaining during compilation. Do you have any alternate suggestions for how to handle this?
Well if the definition of the parameter says that it should be Date | number | 'now'
, then you should be able to use the string "now"
with no need of casting to the type "now"
even in strict mode.
Can you share a sample zip/gist/repo where this is replicable so that I can have a look?
here's a repro link:
https://github.com/goleary/maps-tools-react/tree/gmaps-types-issue/functions
make sure you're in the repo's functions
subfolder when you do the below:
- run
npm install
- copy the contents of
local_node_modules/@types/google__maps/index.d.ts' to
node_modules/@types/google__maps' (to get updated type i'm proposing) npm build
if it complains about node version just usenode ./node_modules/typescript/bin/tsc
apologies for the copy and paste step, I tried to import the typedef from the local_node_modules folder but I was getting all kinds of error so just reverted it to use the installed one which needs to be modified.
While writing this all up I noticed that if I define the request as:
Then I have no issues...is this the best way to handle this? or should typescript know that the the request objection satisfies the requirements without giving it a type?
Got to replicate, thanks.
The answer is yes: departure_time
cannot be any string value, so we added the hardcoded string "now"
, but when you use the string "now" in your application, you should let typescript know that only that value is accepted, otherwise it will infer a generic string type.
Both your proposals are correct, casting to "now" or define request as a DirectionsRequest, but I personally prefer the latter one.
Cool, I'll submit the PR to DefinitelyTyped tomorrow. Is there nothing that maintains this repo as the source for the '.../google__maps' portion of their repo? Seems like duplicating work to submit the same PR twice. (Just out of curiosity)
This repo is just a leftover before I submit it to the DefinitelyTyped group.
gotcha