Chore: improve some types (#95728)

* fix some typings

* fix some more

* more type fixes

* fix some graphite types

* few influx fixes
This commit is contained in:
Ashley Harrison
2024-11-05 09:57:30 +00:00
committed by GitHub
parent 34269a03c0
commit 0fae3579e8
34 changed files with 127 additions and 231 deletions
+10 -6
View File
@@ -1,4 +1,4 @@
import { includes, isDate } from 'lodash';
import { isDate } from 'lodash';
import { TimeZone } from '../types/time';
@@ -12,7 +12,11 @@ import {
ISO_8601,
} from './moment_wrapper';
const units: DurationUnit[] = ['y', 'M', 'w', 'd', 'h', 'm', 's', 'Q'];
const units: string[] = ['y', 'M', 'w', 'd', 'h', 'm', 's', 'Q'] satisfies DurationUnit[];
const isDurationUnit = (value: string): value is DurationUnit => {
return units.includes(value);
};
/**
* Determine if a string contains a relative date time.
@@ -169,11 +173,9 @@ export function parseDateMath(
isFiscal = true;
}
const unit = unitString as DurationUnit;
const unit = unitString;
if (!includes(units, unit)) {
return undefined;
} else {
if (isDurationUnit(unit)) {
if (type === 0) {
if (isFiscal) {
roundToFiscal(fiscalYearStartMonth, result, unit, roundUp);
@@ -189,6 +191,8 @@ export function parseDateMath(
} else if (type === 2) {
result.subtract(num, unit);
}
} else {
return undefined;
}
}
return result;
@@ -1,6 +1,6 @@
import { add, Duration, intervalToDuration, Interval, isAfter } from 'date-fns';
const durationMap: { [key in Required<keyof Duration>]: string[] } = {
const durationMap: Record<string, string[]> = {
years: ['y', 'Y', 'years'],
months: ['M', 'months'],
weeks: ['w', 'W', 'weeks'],
@@ -8,7 +8,7 @@ const durationMap: { [key in Required<keyof Duration>]: string[] } = {
hours: ['h', 'H', 'hours'],
minutes: ['m', 'minutes'],
seconds: ['s', 'S', 'seconds'],
};
} satisfies { [key in keyof Duration]: string[] };
/**
* intervalToAbbreviatedDurationString converts interval to readable duration string
@@ -26,7 +26,7 @@ export function intervalToAbbreviatedDurationString(interval: Interval, includeS
}
const duration = intervalToDuration(interval);
return (Object.entries(duration) as Array<[keyof Duration, number | undefined]>).reduce((str, [unit, value]) => {
return Object.entries(duration).reduce((str, [unit, value]) => {
if (value && value !== 0 && !(unit === 'seconds' && !includeSeconds && str)) {
const padding = str !== '' ? ' ' : '';
return str + `${padding}${value}${durationMap[unit][0]}`;