From 22526aef495f615dd7bc1140cabef1f531b8bfc1 Mon Sep 17 00:00:00 2001 From: WofWca Date: Mon, 1 Jul 2024 16:11:47 +0400 Subject: [PATCH] refactor: safer types To avoid issues like #3990 This ensures that the type and the array always have the same keys However, simply adding an extra property on `SettingsValues` will not cause errors, it will simply keep the `SettingsStoreState` type unchanged. But any runtime changes (e.g. adding or accessing a property without also adding it to the array, or vice versa) will be caught. --- CHANGELOG.md | 1 + src/renderer/stores/settings.ts | 44 +++++++++++++++++---------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 407be3a377..153e89dcb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - use 'Info' and 'Message Info' consistently #3961 - consolidate 'Profile' wording #3963 - Update local help (2024-06-19) +- refactor: safer types #3993 ### Fixed - Fix crash on "Settings" click when not on main screen (e.g. no account selected): hide the "settings" button diff --git a/src/renderer/stores/settings.ts b/src/renderer/stores/settings.ts index 328222d7bf..a910576479 100644 --- a/src/renderer/stores/settings.ts +++ b/src/renderer/stores/settings.ts @@ -10,30 +10,32 @@ export interface SettingsStoreState { accountId: number selfContact: Type.Contact settings: { - sentbox_watch: string - mvbox_move: string - e2ee_enabled: string - addr: string - displayname: string - selfstatus: string - mdns_enabled: string - show_emails: string - bcc_self: string - delete_device_after: string - delete_server_after: string - webrtc_instance: string - download_limit: string - only_fetch_mvbox: string - disable_idle: string - media_quality: string - is_chatmail: '0' | '1' - webxdc_realtime_enabled: string + [P in (typeof settingsKeys)[number]]: { + sentbox_watch: string + mvbox_move: string + e2ee_enabled: string + addr: string + displayname: string + selfstatus: string + mdns_enabled: string + show_emails: string + bcc_self: string + delete_device_after: string + delete_server_after: string + webrtc_instance: string + download_limit: string + only_fetch_mvbox: string + disable_idle: string + media_quality: string + is_chatmail: '0' | '1' + webxdc_realtime_enabled: string + }[P] } desktopSettings: DesktopSettingsType rc: RC_Config } -const settingsKeys: Array = [ +const settingsKeys = [ 'sentbox_watch', 'mvbox_move', 'e2ee_enabled', @@ -52,7 +54,7 @@ const settingsKeys: Array = [ 'media_quality', 'is_chatmail', 'webxdc_realtime_enabled', -] +] as const class SettingsStore extends Store { reducer = { @@ -123,7 +125,7 @@ class SettingsStore extends Store { } const settings = (await BackendRemote.rpc.batchGetConfig( accountId, - settingsKeys + settingsKeys as unknown as Array<(typeof settingsKeys)[number]> )) as SettingsStoreState['settings'] const selfContact = await BackendRemote.rpc.getContact( accountId,