I've been utilizing MomentTimezone for time manipulation within the browser.
My development stack includes TypeScript and Lodash.
In my application, there is an accountTimezone
variable set on the window
object which stores the user's preferred timezone. I am attempting to create a helper function called localMoment()
that will take any of the various signatures of moment.tz()
, adding the window.accountTimezone
as the last argument representing the timezone string.
It appears that partialRight
might be the solution to my issue.
const localMoment = partialRight(moment.tz, window.accountTimezone);
The challenge I'm facing is related to a note in the lodash documentation:
Note: This method doesn't set the "length" property of partially applied functions.
Specifically, when calling
localMoment('2019-08-01 12:00:00')
, TypeScript raises an error mentioning that localMoment()
was given 1 argument instead of the expected zero arguments.
How can I ensure that TypeScript recognizes a call to localMoment()
should mimic a call to moment.tz()
following the MomentTimzone
standards, while addressing the arity confusion caused by using partialRight()
?
I have contemplated an alternative approach like below, but I am uncertain about properly typing the ...args
parameter to satisfy TypeScript requirements.
const localMoment = (...args): Moment => moment.tz(...args, window.accountTimezone);