ts-fix: disambiguate ElectronStore typings

This commit is contained in:
Angelos Bouklis
2024-08-01 10:10:52 +03:00
parent 7b033b5caf
commit 877573532c
2 changed files with 42 additions and 42 deletions

View File

@ -1,9 +1,8 @@
import Store from 'electron-store';
import { deepmergeCustom } from 'deepmerge-ts'; import { deepmergeCustom } from 'deepmerge-ts';
import defaultConfig from './defaults'; import defaultConfig from './defaults';
import store from './store'; import store, { IStore } from './store';
import plugins from './plugins'; import plugins from './plugins';
import { restart } from '@/providers/app-controls'; import { restart } from '@/providers/app-controls';
@ -70,12 +69,11 @@ type Paths<T, D extends number = 10> = [D] extends [never]
: ''; : '';
type SplitKey<K> = K extends `${infer A}.${infer B}` ? [A, B] : [K, string]; type SplitKey<K> = K extends `${infer A}.${infer B}` ? [A, B] : [K, string];
type PathValue<T, K extends string> = SplitKey<K> extends [ type PathValue<T, K extends string> =
infer A extends keyof T, SplitKey<K> extends [infer A extends keyof T, infer B extends string]
infer B extends string,
]
? PathValue<T[A], B> ? PathValue<T[A], B>
: T; : T;
const get = <Key extends Paths<typeof defaultConfig>>(key: Key) => const get = <Key extends Paths<typeof defaultConfig>>(key: Key) =>
store.get(key) as PathValue<typeof defaultConfig, typeof key>; store.get(key) as PathValue<typeof defaultConfig, typeof key>;
@ -86,7 +84,7 @@ export default {
setPartial, setPartial,
setMenuOption, setMenuOption,
edit: () => store.openInEditor(), edit: () => store.openInEditor(),
watch(cb: Parameters<Store['onDidAnyChange']>[0]) { watch(cb: Parameters<IStore['onDidAnyChange']>[0]) {
store.onDidAnyChange(cb); store.onDidAnyChange(cb);
}, },
plugins, plugins,

View File

@ -1,12 +1,14 @@
import Store from 'electron-store'; import Store from 'electron-store';
import Conf from 'conf';
import defaults from './defaults'; import defaults from './defaults';
import { DefaultPresetList, type Preset } from '@/plugins/downloader/types'; import { DefaultPresetList, type Preset } from '@/plugins/downloader/types';
// prettier-ignore
export type IStore = InstanceType<typeof import('conf/dist/source/index').default<Record<string, unknown>>>;
const migrations = { const migrations = {
'>=3.3.0'(store: Conf<Record<string, unknown>>) { '>=3.3.0'(store: IStore) {
const lastfmConfig = store.get('plugins.lastfm') as { const lastfmConfig = store.get('plugins.lastfm') as {
enabled?: boolean; enabled?: boolean;
token?: string; token?: string;
@ -16,9 +18,8 @@ const migrations = {
secret?: string; secret?: string;
}; };
if (lastfmConfig) { if (lastfmConfig) {
let scrobblerConfig = store.get( let scrobblerConfig = store.get('plugins.scrobbler') as
'plugins.scrobbler', | {
) as {
enabled?: boolean; enabled?: boolean;
scrobblers?: { scrobblers?: {
lastfm?: { lastfm?: {
@ -30,7 +31,8 @@ const migrations = {
secret?: string; secret?: string;
}; };
}; };
} | undefined; }
| undefined;
if (!scrobblerConfig) { if (!scrobblerConfig) {
scrobblerConfig = { scrobblerConfig = {
@ -56,7 +58,7 @@ const migrations = {
store.delete('plugins.lastfm'); store.delete('plugins.lastfm');
} }
}, },
'>=3.0.0'(store: Conf<Record<string, unknown>>) { '>=3.0.0'(store: IStore) {
const discordConfig = store.get('plugins.discord') as Record< const discordConfig = store.get('plugins.discord') as Record<
string, string,
unknown unknown
@ -78,14 +80,14 @@ const migrations = {
} }
} }
}, },
'>=2.1.3'(store: Conf<Record<string, unknown>>) { '>=2.1.3'(store: IStore) {
const listenAlong = store.get('plugins.discord.listenAlong'); const listenAlong = store.get('plugins.discord.listenAlong');
if (listenAlong !== undefined) { if (listenAlong !== undefined) {
store.set('plugins.discord.playOnYouTubeMusic', listenAlong); store.set('plugins.discord.playOnYouTubeMusic', listenAlong);
store.delete('plugins.discord.listenAlong'); store.delete('plugins.discord.listenAlong');
} }
}, },
'>=2.1.0'(store: Conf<Record<string, unknown>>) { '>=2.1.0'(store: IStore) {
const originalPreset = store.get('plugins.downloader.preset') as const originalPreset = store.get('plugins.downloader.preset') as
| string | string
| undefined; | undefined;
@ -110,7 +112,7 @@ const migrations = {
store.delete('plugins.downloader.ffmpegArgs'); store.delete('plugins.downloader.ffmpegArgs');
} }
}, },
'>=1.20.0'(store: Conf<Record<string, unknown>>) { '>=1.20.0'(store: IStore) {
store.delete('plugins.visualizer'); // default value is now in the plugin store.delete('plugins.visualizer'); // default value is now in the plugin
if (store.get('plugins.notifications.toastStyle') === undefined) { if (store.get('plugins.notifications.toastStyle') === undefined) {
@ -125,14 +127,14 @@ const migrations = {
store.set('options.likeButtons', 'force'); store.set('options.likeButtons', 'force');
} }
}, },
'>=1.17.0'(store: Conf<Record<string, unknown>>) { '>=1.17.0'(store: IStore) {
store.delete('plugins.picture-in-picture'); // default value is now in the plugin store.delete('plugins.picture-in-picture'); // default value is now in the plugin
if (store.get('plugins.video-toggle.mode') === undefined) { if (store.get('plugins.video-toggle.mode') === undefined) {
store.set('plugins.video-toggle.mode', 'custom'); store.set('plugins.video-toggle.mode', 'custom');
} }
}, },
'>=1.14.0'(store: Conf<Record<string, unknown>>) { '>=1.14.0'(store: IStore) {
if ( if (
typeof store.get('plugins.precise-volume.globalShortcuts') !== 'object' typeof store.get('plugins.precise-volume.globalShortcuts') !== 'object'
) { ) {
@ -144,12 +146,12 @@ const migrations = {
store.set('plugins.video-toggle.enabled', true); store.set('plugins.video-toggle.enabled', true);
} }
}, },
'>=1.13.0'(store: Conf<Record<string, unknown>>) { '>=1.13.0'(store: IStore) {
if (store.get('plugins.discord.listenAlong') === undefined) { if (store.get('plugins.discord.listenAlong') === undefined) {
store.set('plugins.discord.listenAlong', true); store.set('plugins.discord.listenAlong', true);
} }
}, },
'>=1.12.0'(store: Conf<Record<string, unknown>>) { '>=1.12.0'(store: IStore) {
const options = store.get('plugins.shortcuts') as const options = store.get('plugins.shortcuts') as
| Record< | Record<
string, string,
@ -187,12 +189,12 @@ const migrations = {
} }
} }
}, },
'>=1.11.0'(store: Conf<Record<string, unknown>>) { '>=1.11.0'(store: IStore) {
if (store.get('options.resumeOnStart') === undefined) { if (store.get('options.resumeOnStart') === undefined) {
store.set('options.resumeOnStart', true); store.set('options.resumeOnStart', true);
} }
}, },
'>=1.7.0'(store: Conf<Record<string, unknown>>) { '>=1.7.0'(store: IStore) {
const enabledPlugins = store.get('plugins') as string[]; const enabledPlugins = store.get('plugins') as string[];
if (!Array.isArray(enabledPlugins)) { if (!Array.isArray(enabledPlugins)) {
console.warn('Plugins are not in array format, cannot migrate'); console.warn('Plugins are not in array format, cannot migrate');
@ -233,4 +235,4 @@ export default new Store({
}, },
clearInvalidConfig: false, clearInvalidConfig: false,
migrations, migrations,
}); }) as Store & IStore;