Skip to content

Commit

Permalink
change how dayjs types are defined to support chaining extend calls
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/index.d.ts
  • Loading branch information
bengry authored and Ben Grynhaus committed Feb 18, 2019
1 parent 2ccd8f8 commit 47d9957
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
104 changes: 50 additions & 54 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
export = dayjs;
type WithReturnType<TFunction, TExtendedReturnType> = TFunction extends (...args: infer U) => infer R ? (...args: U) => R & TExtendedReturnType : TFunction;

declare function dayjs(config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs
declare const dayjs: dayjs
export default dayjs

declare namespace dayjs {
export type ConfigType = string | number | Date | Dayjs
export type ConfigType = string | number | Date | Dayjs

export type OptionType = { locale: string }
export type OptionType = { locale: string }

export type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
export type UnitTypeSingular = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort;
export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates'
export type UnitType = UnitTypeSingular | UnitTypePlural
export type UnitTypePlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'quarters' | 'years' | 'dates'
export type UnitType = UnitTypeSingular | UnitTypePlural

type OpUnitTypeShort = 'w'
export type OpUnitType = UnitType | "week" | OpUnitTypeShort;

interface DayjsObject {
years: number
months: number
date: number
hours: number
minutes: number
seconds: number
milliseconds: number
}
export type PluginFunc<TPlugin> = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void

class Dayjs {
constructor (config?: ConfigType)
export interface dayjs<TPlugin = {}> {
(config?: ConfigType, option?: OptionType): {} extends TPlugin ? Dayjs : Dayjs & TPlugin

clone(): Dayjs
extend<UPlugin extends object>(plugin: PluginFunc<UPlugin>, option?: ConfigType): dayjs<TPlugin & UPlugin>
locale(arg1: any, arg2?: any): string
isDayjs(d: any): d is Dayjs
unix(t: number): Dayjs
}

export interface DayjsObject {
years: number
months: number
date: number
hours: number
minutes: number
seconds: number
milliseconds: number
}

isValid(): boolean
export class Dayjs {
constructor(config?: ConfigType)

year(): number
clone(): Dayjs

month(): number
isValid(): boolean

date(): number
year(): number

day(): number
month(): number

hour(): number
date(): number

minute(): number
day(): number

second(): number
hour(): number

millisecond(): number
minute(): number

set(unit: UnitType, value: number): Dayjs
second(): number

millisecond(): number

set(unit: UnitType, value: number): Dayjs

add(value: number, unit: OpUnitType): Dayjs

Expand All @@ -59,46 +68,33 @@ declare namespace dayjs {

endOf(unit: OpUnitType): Dayjs

format(template?: string): string
format(template?: string): string

diff(dayjs: ConfigType, unit: OpUnitType, float?: boolean): number

valueOf(): number
valueOf(): number

unix(): number
unix(): number

daysInMonth(): number
daysInMonth(): number

toDate(): Date
toDate(): Date

toArray(): number[]
toArray(): number[]

toJSON(): string
toJSON(): string

toISOString(): string
toISOString(): string

toObject(): DayjsObject
toObject(): DayjsObject

toString(): string
toString(): string

isBefore(dayjs: ConfigType, unit?: OpUnitType): boolean

isSame(dayjs: ConfigType, unit?: OpUnitType): boolean

isAfter(dayjs: ConfigType, unit?: OpUnitType): boolean

locale(arg1: any, arg2?: any): Dayjs
}

export type PluginFunc<TPlugin> = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void

export type dayjsWithPlugin<TPlugin extends object> = WithReturnType<typeof dayjs, TPlugin>;

export function extend<TPlugin extends object>(plugin: PluginFunc<TPlugin>, option?: ConfigType): dayjsWithPlugin<TPlugin>;

export function locale(arg1: any, arg2?: any): string

export function isDayjs(d: any): d is Dayjs

export function unix(t: number): Dayjs
locale(arg1: any, arg2?: any): Dayjs
}
9 changes: 6 additions & 3 deletions test/index.d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ dayjs().isSame(dayjs(), 'hours')

dayjs().isAfter(dayjs(), 'year')


dayjs.extend(relativeTime)().toNow()
dayjs.extend(advancedFormat)().format()
dayjs.extend(buddhistEra)().format()
dayjs.extend(isLeapYear)('2000-01-01').isLeapYear();
dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year');
dayjs.extend(isLeapYear)('2000-01-01').isLeapYear()
dayjs.extend(isBetween)('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'), 'year')

const multiplePluginsDayjs = dayjs.extend(relativeTime).extend(isLeapYear)()
multiplePluginsDayjs.toNow()
multiplePluginsDayjs.isLeapYear()

0 comments on commit 47d9957

Please sign in to comment.