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