diff --git a/superset-frontend/packages/superset-ui-core/src/utils/dates.ts b/superset-frontend/packages/superset-ui-core/src/utils/dates.ts index 9594f15c988..1082b67afe1 100644 --- a/superset-frontend/packages/superset-ui-core/src/utils/dates.ts +++ b/superset-frontend/packages/superset-ui-core/src/utils/dates.ts @@ -17,7 +17,7 @@ * under the License. */ -import dayjs, { Dayjs } from 'dayjs'; +import dayjs, { Dayjs, ConfigType } from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; import calendar from 'dayjs/plugin/calendar'; @@ -40,8 +40,42 @@ dayjs.updateLocale('en', { invalidDate: 'Invalid date', }); -export const extendedDayjs = dayjs; -export type { Dayjs }; +// Extended Dayjs instance type with plugin methods. +// When extending dayjs with a new plugin, add its instance methods here. +export interface ExtendedDayjs extends Dayjs { + // Derived from dayjs/plugin/utc + utc(keepLocalTime?: boolean): ExtendedDayjs; + local(): ExtendedDayjs; + isUTC(): boolean; + // Derived from dayjs/plugin/timezone + tz(timezone?: string, keepLocalTime?: boolean): ExtendedDayjs; + // Derived from dayjs/plugin/relativeTime + fromNow(withoutSuffix?: boolean): string; + toNow(withoutSuffix?: boolean): string; + from(compared: ConfigType, withoutSuffix?: boolean): string; + to(compared: ConfigType, withoutSuffix?: boolean): string; + // Derived from dayjs/plugin/calendar + calendar(referenceTime?: ConfigType, formats?: object): string; +} + +// Type for dayjs factory with all plugins loaded. +// When extending dayjs with a new plugin, add its static methods here. +type DayjsWithPlugins = { + (config?: ConfigType, format?: string, strict?: boolean): ExtendedDayjs; + // Derived from dayjs/plugin/utc + utc(config?: ConfigType, format?: string, strict?: boolean): ExtendedDayjs; + // Derived from dayjs/plugin/timezone + tz: { + (input?: ConfigType, timezone?: string): ExtendedDayjs; + guess(): string; + setDefault(timezone?: string): void; + }; + // Derived from dayjs/plugin/updateLocale + updateLocale(locale: string, config: object): object; +} & Omit; + +export const extendedDayjs = dayjs as unknown as DayjsWithPlugins; +export type { Dayjs, ConfigType }; export const fDuration = function ( t1: number,