UNPKG

9.06 kBTypeScriptView Raw
1import { type constructFromSymbol } from "./constants.js";
2import type { Locale } from "./locale/types.js";
3export type * from "./fp/types.js";
4export type * from "./locale/types.js";
5/**
6 * The argument type.
7 */
8export type DateArg<DateType extends Date> = DateType | number | string;
9/**
10 * Date extension interface that allows to transfer extra properties from
11 * the reference date to the new date. It's useful for extensions like [`TZDate`](https://github.com/date-fns/tz)
12 * that accept a time zone as a constructor argument.
13 */
14export interface ConstructableDate extends Date {
15 [constructFromSymbol]: <DateType extends Date = Date>(
16 value: DateArg<Date> & {},
17 ) => DateType;
18}
19/**
20 * The generic date constructor. Replicates the Date constructor. Used to build
21 * generic functions.
22 *
23 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
24 */
25export interface GenericDateConstructor<DateType extends Date = Date> {
26 /**
27 * The date constructor. Creates date with the current date and time.
28 *
29 * @returns The date instance
30 */
31 new (): DateType;
32 /**
33 * The date constructor. Creates date with the passed date, number of
34 * milliseconds or string to parse.
35 *
36 * @param value - The date, number of milliseconds or string to parse
37 *
38 * @returns The date instance
39 */
40 new (value: DateArg<Date> & {}): DateType;
41 /**
42 * The date constructor. Creates date with the passed date values (year,
43 * month, etc.) Note that the month is 0-indexed.
44 *
45 * @param year - The year
46 * @param month - The month. Note that the month is 0-indexed.
47 * @param date - The day of the month
48 * @param hours - The hours
49 * @param minutes - The minutes
50 * @param seconds - The seconds
51 * @param ms - The milliseconds
52 *
53 * @returns The date instance
54 */
55 new (
56 year: number,
57 month: number,
58 date?: number,
59 hours?: number,
60 minutes?: number,
61 seconds?: number,
62 ms?: number,
63 ): DateType;
64}
65/**
66 * The duration object. Contains the duration in the units specified by the
67 * object.
68 */
69export interface Duration {
70 /** The number of years in the duration */
71 years?: number;
72 /** The number of months in the duration */
73 months?: number;
74 /** The number of weeks in the duration */
75 weeks?: number;
76 /** The number of days in the duration */
77 days?: number;
78 /** The number of hours in the duration */
79 hours?: number;
80 /** The number of minutes in the duration */
81 minutes?: number;
82 /** The number of seconds in the duration */
83 seconds?: number;
84}
85/**
86 * The duration unit type alias.
87 */
88export type DurationUnit = keyof Duration;
89/**
90 * An object that combines two dates to represent the time interval.
91 *
92 * @typeParam StartDate - The start `Date` type.
93 * @typeParam EndDate - The end `Date` type.
94 */
95export interface Interval<
96 StartType extends DateArg<Date> = DateArg<Date>,
97 EndType extends DateArg<Date> = DateArg<Date>,
98> {
99 /** The start of the interval. */
100 start: StartType;
101 /** The end of the interval. */
102 end: EndType;
103}
104/**
105 * A version of {@link Interval} that has both start and end resolved to Date.
106 */
107export type NormalizedInterval<DateType extends Date = Date> = Interval<
108 DateType,
109 DateType
110>;
111/**
112 * The era. Can be either 0 (AD - Anno Domini) or 1 (BC - Before Christ).
113 */
114export type Era = 0 | 1;
115/**
116 * The year quarter. Goes from 1 to 4.
117 */
118export type Quarter = 1 | 2 | 3 | 4;
119/**
120 * The day of the week type alias. Unlike the date (the number of days since
121 * the beginning of the month), which begins with 1 and is dynamic (can go up to
122 * 28, 30, or 31), the day starts with 0 and static (always ends at 6). Look at
123 * it as an index in an array where Sunday is the first element and Saturday
124 * is the last.
125 */
126export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6;
127/**
128 * The month type alias. Goes from 0 to 11, where 0 is January and 11 is
129 * December.
130 */
131export type Month = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
132/**
133 * FirstWeekContainsDate is used to determine which week is the first week of
134 * the year, based on what day the January, 1 is in that week.
135 *
136 * The day in that week can only be 1 (Monday) or 4 (Thursday).
137 *
138 * Please see https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system for more information.
139 */
140export type FirstWeekContainsDate = 1 | 4;
141/**
142 * The date values, used to set or get date object values.
143 */
144export interface DateValues {
145 /** The year */
146 year?: number;
147 /** The month */
148 month?: number;
149 /** The day of the month */
150 date?: number;
151 /** The hours */
152 hours?: number;
153 /** The minutes */
154 minutes?: number;
155 /** The seconds */
156 seconds?: number;
157 /** The milliseconds */
158 milliseconds?: number;
159}
160/**
161 * The number rounding method.
162 */
163export type RoundingMethod = "ceil" | "floor" | "round" | "trunc";
164/**
165 * The ISO string format.
166 *
167 * - basic: Minimal number of separators
168 * - extended: With separators added to enhance human readability
169 */
170export type ISOStringFormat = "extended" | "basic";
171/**
172 * The ISO date representation. Represents which component the string includes,
173 * date, time or both.
174 */
175export type ISOStringRepresentation = "complete" | "date" | "time";
176/**
177 * The step function options. Used to build function options.
178 */
179export interface StepOptions {
180 /** The step to use when iterating */
181 step?: number;
182}
183/**
184 * The week function options. Used to build function options.
185 */
186export interface WeekOptions {
187 /** Which day the week starts on. */
188 weekStartsOn?: Day;
189}
190/**
191 * The first week contains date options. Used to build function options.
192 */
193export interface FirstWeekContainsDateOptions {
194 /** See {@link FirstWeekContainsDate} for more details. */
195 firstWeekContainsDate?: FirstWeekContainsDate;
196}
197/**
198 * The localized function options. Used to build function options.
199 *
200 * @typeParam LocaleFields - The locale fields used in the relevant function. Defines the minimum set of locale fields that must be provided.
201 */
202export interface LocalizedOptions<LocaleFields extends keyof Locale> {
203 /** The locale to use in the function. */
204 locale?: Pick<Locale, LocaleFields>;
205}
206/**
207 * The ISO format function options. Used to build function options.
208 */
209export interface ISOFormatOptions {
210 /** The format to use: basic with minimal number of separators or extended
211 * with separators added to enhance human readability */
212 format?: ISOStringFormat;
213 /** The date representation - what component to format: date, time\
214 * or both (complete) */
215 representation?: ISOStringRepresentation;
216}
217/**
218 * The rounding options. Used to build function options.
219 */
220export interface RoundingOptions {
221 /** The rounding method to use */
222 roundingMethod?: RoundingMethod;
223}
224/**
225 * Additional tokens options. Used to build function options.
226 */
227export interface AdditionalTokensOptions {
228 /** If true, allows usage of the week-numbering year tokens `YY` and `YYYY`.
229 * See: https://date-fns.org/docs/Unicode-Tokens */
230 useAdditionalWeekYearTokens?: boolean;
231 /** If true, allows usage of the day of year tokens `D` and `DD`.
232 * See: https://date-fns.org/docs/Unicode-Tokens */
233 useAdditionalDayOfYearTokens?: boolean;
234}
235/**
236 * Nearest minute type. Goes from 1 to 30, where 1 is the nearest minute and 30
237 * is nearest half an hour.
238 */
239export type NearestMinutes =
240 | 1
241 | 2
242 | 3
243 | 4
244 | 5
245 | 6
246 | 7
247 | 8
248 | 9
249 | 10
250 | 11
251 | 12
252 | 13
253 | 14
254 | 15
255 | 16
256 | 17
257 | 18
258 | 19
259 | 20
260 | 21
261 | 22
262 | 23
263 | 24
264 | 25
265 | 26
266 | 27
267 | 28
268 | 29
269 | 30;
270/**
271 * Nearest hour type. Goes from 1 to 12, where 1 is the nearest hour and 12
272 * is nearest half a day.
273 */
274export type NearestHours = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
275/**
276 * The nearest minutes function options. Used to build function options.
277 *
278 * @deprecated Use {@link NearestToUnitOptions} instead.
279 */
280export type NearestMinutesOptions = NearestToUnitOptions<NearestMinutes>;
281/**
282 * The nearest unit function options. Used to build function options.
283 */
284export interface NearestToUnitOptions<Unit extends number> {
285 /** The nearest unit to round to. E.g. for minutes `15` to round to quarter
286 * hours. */
287 nearestTo?: Unit;
288}
289/**
290 * The context options. Used to build function options.
291 */
292export interface ContextOptions<DateType extends Date> {
293 /**
294 * The context to use in the function. It allows to normalize the arguments
295 * to a specific date instance, which is useful for extensions like [`TZDate`](https://github.com/date-fns/tz).
296 */
297 in?: ContextFn<DateType> | undefined;
298}
299/**
300 /**
301 * The context function type. It's used to normalize the input arguments to
302 * a specific date instance, which is useful for extensions like [`TZDate`](https://github.com/date-fns/tz).
303 */
304export type ContextFn<DateType extends Date> = (
305 value: DateArg<Date> & {},
306) => DateType;
307/**
308 * Resolves passed type or array of types.
309 */
310export type MaybeArray<Type> = Type | Type[];