From 4aca4b1b584a15de1146d929f95c944594032f20 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Sun, 17 Jan 2021 11:46:27 +0300 Subject: [PATCH] fix: Improve `Duration` types (#1338) --- test/plugin/duration.test.js | 2 +- types/index.d.ts | 6 +++--- types/plugin/duration.d.ts | 32 +++++++++++++++++++++----------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index 15e60b925..d248ee33a 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -201,7 +201,7 @@ describe('Years', () => { describe('prettyUnit', () => { const d = dayjs.duration(2, 's') expect(d.toISOString()).toBe('PT2S') - expect(d.as('Second')).toBe(2) + expect(d.as('seconds')).toBe(2) expect(d.get('s')).toBe(2) expect(dayjs.duration({ M: 12, diff --git a/types/index.d.ts b/types/index.d.ts index 46c2f9331..22efc9ebe 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -13,11 +13,11 @@ declare namespace dayjs { export type OptionType = { locale?: string, format?: string, utc?: boolean } | string | string[] - type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' + export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' - type UnitTypeLong = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date' + export type UnitTypeLong = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date' - type UnitTypeLongPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' + export type UnitTypeLongPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' export type UnitType = UnitTypeLong | UnitTypeLongPlural | UnitTypeShort; diff --git a/types/plugin/duration.d.ts b/types/plugin/duration.d.ts index 3695eeaaf..a5b197af5 100644 --- a/types/plugin/duration.d.ts +++ b/types/plugin/duration.d.ts @@ -1,15 +1,22 @@ import { PluginFunc } from 'dayjs' +import { OpUnitType, UnitTypeLongPlural } from "../index"; declare const plugin: PluginFunc export as namespace plugin; export = plugin declare namespace plugin { - type DurationInputType = string | number | object - type DurationAddType = number | object | Duration + type DurationUnitsObjectType = Partial<{ + [unit in Exclude | "weeks"]: number + }>; + type DurationUnitType = Exclude + type CreateDurationType = + ((units: DurationUnitsObjectType) => Duration) + & ((time: number, unit?: DurationUnitType) => Duration) + & ((ISO_8601: string) => Duration) interface Duration { - new (input: DurationInputType, unit?: string, locale?: string): Duration + new (input: string | number | object, unit?: string, locale?: string): Duration clone(): Duration @@ -39,13 +46,13 @@ declare namespace plugin { years(): number asYears(): number - as(unit: string): number + as(unit: DurationUnitType): number - get(unit: string): number + get(unit: DurationUnitType): number - add(input: DurationAddType, unit? : string): Duration - - subtract(input: DurationAddType, unit? : string): Duration + add: CreateDurationType; + + subtract: CreateDurationType toJSON(): string @@ -59,10 +66,13 @@ declare namespace plugin { declare module 'dayjs' { interface Dayjs { - add(value: plugin.Duration): Dayjs - subtract(value: plugin.Duration): Dayjs + add(duration: plugin.Duration): Dayjs + subtract(duration: plugin.Duration): Dayjs } - export function duration(input?: plugin.DurationInputType , unit?: string): plugin.Duration + /** + * @param time If unit is not present, time treated as number of milliseconds + */ + export const duration: plugin.CreateDurationType; export function isDuration(d: any): d is plugin.Duration }