From f79c6a985f3e371261759015db462cac7348e934 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Mon, 18 Feb 2019 22:54:21 +0200 Subject: [PATCH] add types for `CustomParseFormat` plugin, and allow creating types for static plugins --- src/index.d.ts | 20 ++++++++++++++------ src/plugin/customParseFormat/index.d.ts | 8 ++++++++ test/{index.d.test.ts => index.test.d.ts} | 4 ++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/plugin/customParseFormat/index.d.ts rename test/{index.d.test.ts => index.test.d.ts} (88%) diff --git a/src/index.d.ts b/src/index.d.ts index 0b8388339..02f790bbf 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -14,7 +14,15 @@ declare namespace dayjs { type OpUnitTypeShort = 'w' type OpUnitType = UnitType | "week" | OpUnitTypeShort; - type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void + type PluginFunc = (option: ConfigType, dayjsClass: InstanceType, dayjsFactory: dayjs) => void; + + /** + * Branding for static plugins, to enable better type inference. + * `extend` from this to make the plugin static + */ + interface StaticPlugin { + __staticPlugin: never; + } } interface dayjs { @@ -25,7 +33,7 @@ interface dayjs { extend( plugin: dayjs.PluginFunc, option?: dayjs.ConfigType - ): dayjs; + ): UPlugin extends dayjs.StaticPlugin ? (dayjs & UPlugin) : dayjs; locale(arg1: any, arg2?: any): string; isDayjs(d: any): d is Dayjs; unix(t: number): Dayjs; @@ -76,7 +84,7 @@ declare class Dayjs { format(template?: string): string; - diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number + diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number valueOf(): number; @@ -96,11 +104,11 @@ declare class Dayjs { toString(): string; - isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean + isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean - isSame(dayjs: ConfigType, unit?: OpUnitType): boolean + isSame(dayjs: ConfigType, unit?: OpUnitType): boolean - isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean + isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean locale(arg1: any, arg2?: any): Dayjs; } diff --git a/src/plugin/customParseFormat/index.d.ts b/src/plugin/customParseFormat/index.d.ts new file mode 100644 index 000000000..91ff52fc7 --- /dev/null +++ b/src/plugin/customParseFormat/index.d.ts @@ -0,0 +1,8 @@ +import { ConfigType, PluginFunc, StaticPlugin } from '../../index' + +interface CustomParseFormatPlugin extends StaticPlugin { + (input: string, format: string): this; +} + +declare const pluginFn: PluginFunc; +export default pluginFn diff --git a/test/index.d.test.ts b/test/index.test.d.ts similarity index 88% rename from test/index.d.test.ts rename to test/index.test.d.ts index 2054ea8f1..10a9a7b9f 100644 --- a/test/index.d.test.ts +++ b/test/index.test.d.ts @@ -4,6 +4,7 @@ import advancedFormat from '../src/plugin/advancedFormat' import buddhistEra from '../src/plugin/buddhistEra' import isLeapYear from '../src/plugin/isLeapYear' import isBetween from '../src/plugin/isBetween' +import customParseFormat from '../src/plugin/customParseFormat' dayjs() @@ -90,3 +91,6 @@ dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25' const multiplePluginsDayjs = dayjs.extend(relativeTime).extend(isLeapYear)() multiplePluginsDayjs.toNow() multiplePluginsDayjs.isLeapYear() + +const dayjsWithCustomParseFormat = dayjs.extend(customParseFormat); +dayjsWithCustomParseFormat('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z');