From eca28ac2f36c58cc0031dcc6dfb808656636aedf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Telaty=C5=84ski?= <7t3chguy@gmail.com> Date: Tue, 28 Feb 2023 10:24:59 +0000 Subject: [PATCH] Apply strictNullChecks to src/settings/* (#10252 * Apply strictNullChecks to src/settings/* * Fix inherited types --- src/accessibility/KeyboardShortcuts.ts | 2 +- src/settings/Settings.tsx | 2 +- src/settings/SettingsStore.ts | 16 +++++++------- .../controllers/SlidingSyncController.ts | 2 +- src/settings/watchers/ThemeWatcher.ts | 22 ++++++++----------- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/accessibility/KeyboardShortcuts.ts b/src/accessibility/KeyboardShortcuts.ts index 3011a5b5bd..bcd720ee21 100644 --- a/src/accessibility/KeyboardShortcuts.ts +++ b/src/accessibility/KeyboardShortcuts.ts @@ -154,7 +154,7 @@ export enum KeyBindingAction { ToggleHiddenEventVisibility = "KeyBinding.toggleHiddenEventVisibility", } -type KeyboardShortcutSetting = IBaseSetting; +type KeyboardShortcutSetting = Omit, "supportedLevels">; // TODO: We should figure out what to do with the keyboard shortcuts that are not handled by KeybindingManager export type IKeyboardShortcuts = Partial>; diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx index 9e9b920a8d..83a9cfcc99 100644 --- a/src/settings/Settings.tsx +++ b/src/settings/Settings.tsx @@ -126,7 +126,7 @@ export interface IBaseSetting { // The supported levels are required. Preferably, use the preset arrays // at the top of this file to define this rather than a custom array. - supportedLevels?: SettingLevel[]; + supportedLevels: SettingLevel[]; // Required. Can be any data type. The value specified here should match // the data being stored (ie: if a boolean is used, the setting should diff --git a/src/settings/SettingsStore.ts b/src/settings/SettingsStore.ts index bcc7b14509..962078ec7e 100644 --- a/src/settings/SettingsStore.ts +++ b/src/settings/SettingsStore.ts @@ -45,13 +45,13 @@ const defaultWatchManager = new WatchManager(); const defaultSettings: Record = {}; const invertedDefaultSettings: Record = {}; const featureNames: string[] = []; -for (const key of Object.keys(SETTINGS)) { - defaultSettings[key] = SETTINGS[key].default; - if (SETTINGS[key].isFeature) featureNames.push(key); - if (SETTINGS[key].invertedSettingName) { - // Invert now so that the rest of the system will invert it back - // to what was intended. - invertedDefaultSettings[SETTINGS[key].invertedSettingName] = !SETTINGS[key].default; +for (const key in SETTINGS) { + const setting = SETTINGS[key]; + defaultSettings[key] = setting.default; + if (setting.isFeature) featureNames.push(key); + if (setting.invertedSettingName) { + // Invert now so that the rest of the system will invert it back to what was intended. + invertedDefaultSettings[setting.invertedSettingName] = !setting.default; } } @@ -250,7 +250,7 @@ export default class SettingsStore { if (roomId === null) { // Unregister all existing watchers and register the new one rooms.forEach((roomId) => { - SettingsStore.unwatchSetting(this.monitors.get(settingName)!.get(roomId)); + SettingsStore.unwatchSetting(this.monitors.get(settingName)!.get(roomId)!); }); this.monitors.get(settingName)!.clear(); registerWatcher(); diff --git a/src/settings/controllers/SlidingSyncController.ts b/src/settings/controllers/SlidingSyncController.ts index fdbea7bda0..56149f2099 100644 --- a/src/settings/controllers/SlidingSyncController.ts +++ b/src/settings/controllers/SlidingSyncController.ts @@ -29,7 +29,7 @@ export default class SlidingSyncController extends SettingController { } public async onChange(): Promise { - PlatformPeg.get().reload(); + PlatformPeg.get()?.reload(); } public get settingDisabled(): boolean { diff --git a/src/settings/watchers/ThemeWatcher.ts b/src/settings/watchers/ThemeWatcher.ts index 6ef9a24a11..79a63f5470 100644 --- a/src/settings/watchers/ThemeWatcher.ts +++ b/src/settings/watchers/ThemeWatcher.ts @@ -53,20 +53,16 @@ export default class ThemeWatcher { public start(): void { this.themeWatchRef = SettingsStore.watchSetting("theme", null, this.onChange); this.systemThemeWatchRef = SettingsStore.watchSetting("use_system_theme", null, this.onChange); - if (this.preferDark.addEventListener) { - this.preferDark.addEventListener("change", this.onChange); - this.preferLight.addEventListener("change", this.onChange); - this.preferHighContrast.addEventListener("change", this.onChange); - } + this.preferDark.addEventListener("change", this.onChange); + this.preferLight.addEventListener("change", this.onChange); + this.preferHighContrast.addEventListener("change", this.onChange); this.dispatcherRef = dis.register(this.onAction); } public stop(): void { - if (this.preferDark.addEventListener) { - this.preferDark.removeEventListener("change", this.onChange); - this.preferLight.removeEventListener("change", this.onChange); - this.preferHighContrast.removeEventListener("change", this.onChange); - } + this.preferDark.removeEventListener("change", this.onChange); + this.preferLight.removeEventListener("change", this.onChange); + this.preferHighContrast.removeEventListener("change", this.onChange); if (this.systemThemeWatchRef) SettingsStore.unwatchSetting(this.systemThemeWatchRef); if (this.themeWatchRef) SettingsStore.unwatchSetting(this.themeWatchRef); if (this.dispatcherRef) dis.unregister(this.dispatcherRef); @@ -144,14 +140,14 @@ export default class ThemeWatcher { return SettingsStore.getValue("theme"); } - private themeBasedOnSystem(): string { - let newTheme: string; + private themeBasedOnSystem(): string | undefined { + let newTheme: string | undefined; if (this.preferDark.matches) { newTheme = "dark"; } else if (this.preferLight.matches) { newTheme = "light"; } - if (this.preferHighContrast.matches) { + if (newTheme && this.preferHighContrast.matches) { const hcTheme = findHighContrastTheme(newTheme); if (hcTheme) { newTheme = hcTheme;