Skip to content

Commit

Permalink
add types for CustomParseFormat plugin, and allow creating types fo…
Browse files Browse the repository at this point in the history
…r static plugins
  • Loading branch information
Ben Grynhaus committed Feb 18, 2019
1 parent 2cde4d7 commit f79c6a9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ declare namespace dayjs {
type OpUnitTypeShort = 'w'
type OpUnitType = UnitType | "week" | OpUnitTypeShort;

type PluginFunc<TPlugin> = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void
type PluginFunc<TPlugin> = (option: ConfigType, dayjsClass: InstanceType<Dayjs>, 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<TPlugin = {}> {
Expand All @@ -25,7 +33,7 @@ interface dayjs<TPlugin = {}> {
extend<UPlugin extends object>(
plugin: dayjs.PluginFunc<UPlugin>,
option?: dayjs.ConfigType
): dayjs<TPlugin & UPlugin>;
): UPlugin extends dayjs.StaticPlugin ? (dayjs<TPlugin> & UPlugin) : dayjs<TPlugin & UPlugin>;
locale(arg1: any, arg2?: any): string;
isDayjs(d: any): d is Dayjs;
unix(t: number): Dayjs;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
8 changes: 8 additions & 0 deletions src/plugin/customParseFormat/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ConfigType, PluginFunc, StaticPlugin } from '../../index'

interface CustomParseFormatPlugin extends StaticPlugin {
(input: string, format: string): this;
}

declare const pluginFn: PluginFunc<CustomParseFormatPlugin>;
export default pluginFn
4 changes: 4 additions & 0 deletions test/index.d.test.ts → test/index.test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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');

0 comments on commit f79c6a9

Please sign in to comment.