From 3f7c2631eda700f7a422046480c3ecc5c72289a0 Mon Sep 17 00:00:00 2001 From: AlliotTech <24980252+AlliotTech@users.noreply.github.com> Date: Fri, 6 Feb 2026 15:26:19 +0800 Subject: [PATCH] refactor: optimize buildSettingsPatch function by using const assertions for keys --- lib/settings.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/settings.ts b/lib/settings.ts index befbf45..5263478 100644 --- a/lib/settings.ts +++ b/lib/settings.ts @@ -2,8 +2,7 @@ import { AppConfig } from "@/lib/types"; // Keep keys in sync with AppConfig when adding new settings fields. export function buildSettingsPatch(original: AppConfig, next: AppConfig): Partial { - const patch: Partial = {}; - const keys: Array = [ + const rootKeys = [ "theme", "layout", "dashboard_display", @@ -12,41 +11,46 @@ export function buildSettingsPatch(original: AppConfig, next: AppConfig): Partia "file_size_si_units", "powered_on_hours_unit", "line_stroke", - ]; - keys.forEach((key) => { + ] as const; + type RootKey = (typeof rootKeys)[number]; + const patch: Partial = {}; + rootKeys.forEach((key) => { const nextValue = next[key]; if (nextValue !== undefined && nextValue !== original[key]) { - patch[key] = nextValue; + (patch as Record)[key] = nextValue; } }); - const metricsKeys: Array> = [ + const metricsKeys = [ "notify_level", "status_filter_attributes", "status_threshold", "repeat_notifications", - ]; + ] as const; + type MetricsKey = (typeof metricsKeys)[number]; const metricsPatch: Partial> = {}; metricsKeys.forEach((key) => { const nextValue = next.metrics?.[key]; const originalValue = original.metrics?.[key]; if (nextValue !== undefined && nextValue !== originalValue) { - metricsPatch[key] = nextValue; + (metricsPatch as Record[MetricsKey]>)[key] = + nextValue; } }); if (Object.keys(metricsPatch).length > 0) { patch.metrics = metricsPatch; } - const collectorKeys: Array> = [ - "discard_sct_temp_history", - ]; + const collectorKeys = ["discard_sct_temp_history"] as const; + type CollectorKey = (typeof collectorKeys)[number]; const collectorPatch: Partial> = {}; collectorKeys.forEach((key) => { const nextValue = next.collector?.[key]; const originalValue = original.collector?.[key]; if (nextValue !== undefined && nextValue !== originalValue) { - collectorPatch[key] = nextValue; + (collectorPatch as Record[CollectorKey]>)[ + key + ] = nextValue; } }); if (Object.keys(collectorPatch).length > 0) {