Tuesday, 20 August 2019

Working around unset "length" property on partial functions created with lodash's partialRight

I'm working with MomentTimezone for time manipulation in the browser.

I am using TypeScript and Lodash too.

I have some accountTimezone set on the window containing the authenticated user's preferred timezone. I am trying to create a helper method localMoment() that will accept any of the many signatures of moment.tz(), appending this window.accountTimezone as the final timezone: string argument.

It seems partialRight may be what I'm looking for.

const localMoment = partialRight(moment.tz, window.accountTimezone);

The problem I'm having has to do with this note from the lodash docs:

Note: This method doesn't set the "length" property of partially applied functions.

Specifically, for a call like localMoment('2019-08-01 12:00:00'), TypeScript complains that localMoment() was provided 1 argument, but expects zero.

How can I keep TypeScript happily understanding a call to localMoment() should look like a call to moment.tz() via the MomentTimzone interface, while avoiding this arity confusion from the use of partialRight()?


I considered something like this as an alternative, but don't know how to type ...args to keep TypeScript happy.

const localMoment = (...args): Moment => moment.tz(...args, window.accountTimezone);



from Working around unset "length" property on partial functions created with lodash's partialRight

No comments:

Post a Comment