Working around unset "length" property on partial functions created with lodash's partialRight
There is no clean way to do this. You either have to opt out of typing or redeclare an interface of your own.
Typescript himself can't do this and just opt for the "good-enough" solution of declaring a bunch of different signatures: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/common/function.d.ts#L1147
Even if you somehow manage to manipulate the typescript interface, I doubt you could handle the other methods (zone
, add
, link
, ...) which don't have a timezone parameter:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/moment-timezone/moment-timezone.d.ts#L20
The best you can achieve is avoiding duplicating the whole interface using the Pick
utility type:
type CurriedMomentTimezone = Pick<moment.MomentTimezone, 'zone' | 'add' | 'link' | 'load' | 'names' | 'guess' | 'setDefault'> & {
(): moment.Moment;
(date: number): moment.Moment;
(date: number[]): moment.Moment;
(date: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, strict: boolean): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, language: string): moment.Moment;
(date: string, format: moment.MomentFormatSpecification, language: string, strict: boolean): moment.Moment;
(date: Date): moment.Moment;
(date: moment.Moment): moment.Moment;
(date: any): moment.Moment;
}
localMoment = _.partialRight(moment.tz, this.accountTimezone) as CurriedMomentTimezone;
These signatures make TypeScript happy:
const localMoment = (...args: any[]): moment.Moment => moment.tz.apply(this, [...args, this.accountTimezone]);
const localMoment2 = _.partialRight(moment.tz, this.accountTimezone) as (...args: any[]) => moment.Moment;
Stackblitz: https://stackblitz.com/edit/typescript-r1h2tq?embed=1&file=index.ts
Since one moment.tz signature accepts a parameter of any
type, it's not much use to create a stricter type than any[]
unless you create local overloads for every moment.tz overload.