ShahinSorkh
ShahinSorkh

Reputation: 651

Augment dayjs with dayjs-recur plugin types

So here is the situation, I am using dayjs-recur plugin of dayjs in my project, but the tsc complains that it cannot find type declarations. So I resolved the issue using the following piece of code in dayjs-recur.d.ts:

declare module 'dayjs-recur' {
  import { PluginFunc } from 'dayjs';
  const defaultExport: PluginFunc<unknown>;
  export = defaultExport;
}

Though it is not enough to satisfy the LSP, I must also augment dayjs namespace. By looking up typescript documentation I came up with the following snippet:

declare module 'dayjs-recur' {
  import { Dayjs, PluginFunc } from 'dayjs';

  interface RecurPlugin {
    every(...args: any): RecurPlugin;
    daysOfMonth(): RecurPlugin;
    fromDate(date: any): RecurPlugin;
    next(any: any): Dayjs;
    all(): Dayjs[];
  }

  const defaultExport: PluginFunc<unknown>;
  export = defaultExport;

  namespace dayjs {
    export function recur(...args: any): RecurPlugin;
  }
}

But it doesn't seem to do anything. Any suggestions?

Upvotes: 0

Views: 605

Answers (1)

ShahinSorkh
ShahinSorkh

Reputation: 651

Here is what I came up with finally:

declare module 'dayjs-recur' {
  interface DayjsRecur {
    ...
  }

  declare module 'dayjs' { // <--- HERE is what I was doing wrong
    export function recur(...args: any): DayjsRecur;
  }

  const defaultExport: PluginFunc<unknown>;
  export = defaultExport;
}

This is my final type declaration for future reference:

// dayjs-recur.d.ts

declare module 'dayjs-recur' {
  import { Dayjs, PluginFunc } from 'dayjs';

  interface Recurrence {
    every(unit: number | string, measurementType?: string): Recurrence;
    every(units: number[] | string[], measurementType?: string): Recurrence;
    day(unit?: string): Recurrence;
    days(unit?: string): Recurrence;
    week(unit?: string): Recurrence;
    weeks(unit?: string): Recurrence;
    month(unit?: string): Recurrence;
    months(unit?: string): Recurrence;
    year(unit?: string): Recurrence;
    years(unit?: string): Recurrence;
    dayOfWeek(unit?: string): Recurrence;
    daysOfWeek(unit?: string): Recurrence;
    dayOfMonth(unit?: string): Recurrence;
    daysOfMonth(unit?: string): Recurrence;
    weekOfMonth(unit?: string): Recurrence;
    weeksOfMonth(unit?: string): Recurrence;
    weekOfYear(unit?: string): Recurrence;
    weeksOfYear(unit?: string): Recurrence;
    monthOfYear(unit?: string): Recurrence;
    monthsOfYear(unit?: string): Recurrence;
    forget(date: string): Recurrence;
    except(date: string): Recurrence;
    matches(date: string, outbound?: boolean): boolean;
    fromDate(date: string | Dayjs): Recurrence;
    next(count?: number): Dayjs | Dayjs[];
    previous(count?: number): Dayjs | Dayjs[];
    all(): Dayjs[];
  }

  declare module 'dayjs' {
    export function recur(): Recurrence;
    export function recur(
      start?: string | Dayjs,
      end?: string | Dayjs,
    ): Recurrence;
    export function recur(options: {
      start: string | Dayjs;
      end: string | Dayjs;
    }): Recurrence;

    interface Dayjs {
      recur(): Recurrence;
      recur(start?: string | Dayjs, end?: string | Dayjs): Recurrence;
      recur(options: {
        start: string | Dayjs;
        end: string | Dayjs;
      }): Recurrence;
    }
  }

  const defaultExport: PluginFunc<unknown>;
  export = defaultExport;
}

Update

You can install type definitions with the following command now: (Thanks to @leog in the comments section)

$ npm i -D @types/dayjs-recur

Upvotes: 2

Related Questions