UNPKG

2.06 kBTypeScriptView Raw
1import type { ContextOptions, DateArg, NormalizedInterval } from "./types.js";
2/**
3 * The {@link interval} function options.
4 */
5export interface IntervalOptions<ContextDate extends Date = Date>
6 extends ContextOptions<ContextDate> {
7 /** Asserts that the interval is positive (start is after the end). */
8 assertPositive?: boolean;
9}
10/**
11 * The {@link interval} function result type. It resolves the proper data type.
12 * It uses the first argument date object type, starting from the start argument,
13 * then the end interval date. If a context function is passed, it uses the context
14 * function return type.
15 */
16export type IntervalResult<
17 StartDate extends DateArg<Date>,
18 EndDate extends DateArg<Date>,
19 Options extends IntervalOptions | undefined = undefined,
20> = NormalizedInterval<
21 Options extends IntervalOptions<infer DateType extends Date>
22 ? DateType
23 : StartDate extends Date
24 ? StartDate
25 : EndDate extends Date
26 ? EndDate
27 : Date
28>;
29/**
30 * @name interval
31 * @category Interval Helpers
32 * @summary Creates an interval object and validates its values.
33 *
34 * @description
35 * Creates a normalized interval object and validates its values. If the interval is invalid, an exception is thrown.
36 *
37 * @typeParam StartDate - Start date type.
38 * @typeParam EndDate - End date type.
39 * @typeParam Options - Options type.
40 *
41 * @param start - The start of the interval.
42 * @param end - The end of the interval.
43 * @param options - The options object.
44 *
45 * @throws `Start date is invalid` when `start` is invalid.
46 * @throws `End date is invalid` when `end` is invalid.
47 * @throws `End date must be after start date` when end is before `start` and `options.assertPositive` is true.
48 *
49 * @returns The normalized and validated interval object.
50 */
51export declare function interval<
52 StartDate extends DateArg<Date>,
53 EndDate extends DateArg<Date>,
54 Options extends IntervalOptions | undefined = undefined,
55>(
56 start: StartDate,
57 end: EndDate,
58 options?: Options,
59): IntervalResult<StartDate, EndDate, Options>;