From ba50a776eedc019f1f5f74b173571e99539d0b36 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Sat, 19 Jan 2019 21:56:00 -0700 Subject: [PATCH] [@types/luxon] Fix and simplify Duration units. The units that can be passed as keys in a an object for methods like `plus` are the same unit that can be passed by string in methods like `as`. And in both cases both singular and plural versions are accepted as they [get normalized](https://github.com/moment/luxon/blob/5c449fef840f8023e2444cbb3497272078623a2c/src/duration.js#L233). --- types/luxon/index.d.ts | 34 ++++++++++++++-------------------- types/luxon/luxon-tests.ts | 3 ++- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/types/luxon/index.d.ts b/types/luxon/index.d.ts index 22305ebf84..f19a7aab55 100644 --- a/types/luxon/index.d.ts +++ b/types/luxon/index.d.ts @@ -244,7 +244,9 @@ export class DateTime { minus(duration: Duration | number | DurationObject): DateTime; plus(duration: Duration | number | DurationObject): DateTime; reconfigure(properties: LocaleOptions): DateTime; - resolvedLocaleOpts(options?: DateTimeFormatOptions): Intl.ResolvedDateTimeFormatOptions; + resolvedLocaleOpts( + options?: DateTimeFormatOptions + ): Intl.ResolvedDateTimeFormatOptions; set(values: DateObjectUnits): DateTime; setLocale(locale: string): DateTime; setZone(zone: string | Zone, options?: ZoneOptions): DateTime; @@ -283,37 +285,29 @@ export interface DurationOptions { } export interface DurationObjectUnits { + year?: number; years?: number; + quarter?: number; + quarters?: number; + month?: number; months?: number; + week?: number; weeks?: number; + day?: number; days?: number; + hour?: number; hours?: number; + minute?: number; minutes?: number; + second?: number; seconds?: number; + millisecond?: number; milliseconds?: number; } export interface DurationObject extends DurationObjectUnits, DurationOptions {} -export type DurationUnit = - | "year" - | "years" - | "quarter" - | "quarters" - | "month" - | "months" - | "week" - | "weeks" - | "day" - | "days" - | "hour" - | "hours" - | "minute" - | "minutes" - | "second" - | "seconds" - | "millisecond" - | "milliseconds"; +export type DurationUnit = keyof DurationObjectUnits; export interface DurationToFormatOptions extends DateTimeFormatOptions { floor?: boolean; diff --git a/types/luxon/luxon-tests.ts b/types/luxon/luxon-tests.ts index 094c1b811a..3cadfa5779 100644 --- a/types/luxon/luxon-tests.ts +++ b/types/luxon/luxon-tests.ts @@ -118,6 +118,7 @@ const { input, result, zone } = DateTime.fromFormatExplain('Aug 6 1982', 'MMMM d /* Duration */ const dur = Duration.fromObject({ hours: 2, minutes: 7 }); dt.plus(dur); // $ExpectType DateTime +dt.plus({ quarters: 2, month: 1 }); // $ExpectType DateTime dur.hours; // $ExpectType number dur.minutes; // $ExpectType number dur.seconds; // $ExpectType number @@ -274,7 +275,7 @@ end.diffNow(['months', 'days']); // $ExpectType Duration dur.as('days'); // $ExpectType number dur.shiftTo('days').toObject().days; // $ExpectType number | undefined -dur.shiftTo('weeks', 'hours').toObject().weeks; // $ExpectType number | undefined +dur.shiftTo('week', 'hours').toObject().weeks; // $ExpectType number | undefined DateTime.local().plus(dur.shiftTo('milliseconds')).year; // $ExpectType number Duration.fromISO('PY23', { conversionAccuracy: 'longterm' }); // $ExpectType Duration