Skip to content

Commit f744621

Browse files
authored
Type fixes for time adapters (#12010)
Specific changes: * `formats` is defined as an arbitrary record. In practice, it maps from `TimeUnit` and `'datetime'` to the specific date library's format strings. * `parse` and `format` were defined as requiring `TimeUnit`, but they actually take date library format strings. (E.g., it's up to the caller to look up format strings via `formats()` or user parameters.) * `endOf` is never passed `isoWeek` (`isoWeek` isn't a normal `TimeUnit`, it's only used as a special case to `startOf`), and [chartjs-adapter-date-fns](https://github.com/chartjs/chartjs-adapter-date-fns/blob/v3.0.0/src/index.js#L101) doesn't support it. * The constructor's options parameter is optional. * `weekday` is documented as allowing a boolean (true means to start on Monday). * `export default { _date: DateAdapterBase }` meant that `new _date()` returns a `DateAdapterBase` instance, whose methods take no parameters. Since it can be overridden at runtime, I replaced it with a more generic constructor + static methods.
1 parent 246c5c7 commit f744621

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/core/core.adapters.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ export interface DateAdapter<T extends AnyObject = AnyObject> {
1919
* Returns a map of time formats for the supported formatting units defined
2020
* in Unit as well as 'datetime' representing a detailed date/time string.
2121
*/
22-
formats(this: DateAdapter<T>): Record<string, string>;
22+
formats(this: DateAdapter<T>): Record<TimeUnit | 'datetime', string>;
2323
/**
2424
* Parses the given `value` and return the associated timestamp.
2525
* @param value - the value to parse (usually comes from the data)
2626
* @param [format] - the expected data format
2727
*/
28-
parse(this: DateAdapter<T>, value: unknown, format?: TimeUnit): number | null;
28+
parse(this: DateAdapter<T>, value: unknown, format?: string): number | null;
2929
/**
3030
* Returns the formatted date in the specified `format` for a given `timestamp`.
3131
* @param timestamp - the timestamp to format
3232
* @param format - the date/time token
3333
*/
34-
format(this: DateAdapter<T>, timestamp: number, format: TimeUnit): string;
34+
format(this: DateAdapter<T>, timestamp: number, format: string): string;
3535
/**
3636
* Adds the specified `amount` of `unit` to the given `timestamp`.
3737
* @param timestamp - the input timestamp
@@ -53,13 +53,13 @@ export interface DateAdapter<T extends AnyObject = AnyObject> {
5353
* @param [weekday] - the ISO day of the week with 1 being Monday
5454
* and 7 being Sunday (only needed if param *unit* is `isoWeek`).
5555
*/
56-
startOf(this: DateAdapter<T>, timestamp: number, unit: TimeUnit | 'isoWeek', weekday?: number): number;
56+
startOf(this: DateAdapter<T>, timestamp: number, unit: TimeUnit | 'isoWeek', weekday?: number | boolean): number;
5757
/**
5858
* Returns end of `unit` for the given `timestamp`.
5959
* @param timestamp - the input timestamp
6060
* @param unit - the unit as string
6161
*/
62-
endOf(this: DateAdapter<T>, timestamp: number, unit: TimeUnit | 'isoWeek'): number;
62+
endOf(this: DateAdapter<T>, timestamp: number, unit: TimeUnit): number;
6363
}
6464

6565
function abstract<T = void>(): T {
@@ -92,14 +92,14 @@ class DateAdapterBase implements DateAdapter {
9292

9393
readonly options: AnyObject;
9494

95-
constructor(options: AnyObject) {
95+
constructor(options?: AnyObject) {
9696
this.options = options || {};
9797
}
9898

9999
// eslint-disable-next-line @typescript-eslint/no-empty-function
100100
init() {}
101101

102-
formats(): Record<string, string> {
102+
formats(): Record<TimeUnit | 'datetime', string> {
103103
return abstract();
104104
}
105105

@@ -129,5 +129,10 @@ class DateAdapterBase implements DateAdapter {
129129
}
130130

131131
export default {
132-
_date: DateAdapterBase
132+
_date: DateAdapterBase as {
133+
new (options?: AnyObject): DateAdapter;
134+
override<T extends AnyObject = AnyObject>(
135+
members: Partial<Omit<DateAdapter<T>, 'options'>>
136+
): void;
137+
}
133138
};

src/scales/scale.time.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function parse(scale, input) {
5959
// Only parse if it's not a timestamp already
6060
if (!isFinite(value)) {
6161
value = typeof parser === 'string'
62-
? adapter.parse(value, /** @type {Unit} */ (parser))
62+
? adapter.parse(value, parser)
6363
: adapter.parse(value);
6464
}
6565

0 commit comments

Comments
 (0)