diff options
author | V <vendicated@riseup.net> | 2023-06-21 02:00:38 +0200 |
---|---|---|
committer | V <vendicated@riseup.net> | 2023-06-21 02:00:59 +0200 |
commit | 3020fcc9bb1af9ae40e84c08c3b44ea66344b27b (patch) | |
tree | dabe91597853856e5a3e0ed892bf1378271fdc7d | |
parent | bc0de3926c5b337a8be1fd0407a68a83c28a0c58 (diff) | |
download | Vencord-3020fcc9bb1af9ae40e84c08c3b44ea66344b27b.tar.gz Vencord-3020fcc9bb1af9ae40e84c08c3b44ea66344b27b.tar.bz2 Vencord-3020fcc9bb1af9ae40e84c08c3b44ea66344b27b.zip |
[skip ci] Improve typings for settings.withPrivateSettings
-rw-r--r-- | src/api/Settings.ts | 14 | ||||
-rw-r--r-- | src/utils/types.ts | 16 |
2 files changed, 19 insertions, 11 deletions
diff --git a/src/api/Settings.ts b/src/api/Settings.ts index 2f78668..709050f 100644 --- a/src/api/Settings.ts +++ b/src/api/Settings.ts @@ -254,8 +254,12 @@ export function migratePluginSettings(name: string, ...oldNames: string[]) { } } -export function definePluginSettings<D extends SettingsDefinition, C extends SettingsChecks<D>>(def: D, checks?: C) { - const definedSettings: DefinedSettings<D> = { +export function definePluginSettings< + Def extends SettingsDefinition, + Checks extends SettingsChecks<Def>, + PrivateSettings extends object = {} +>(def: Def, checks?: Checks) { + const definedSettings: DefinedSettings<Def, Checks, PrivateSettings> = { get store() { if (!definedSettings.pluginName) throw new Error("Cannot access settings before plugin is initialized"); return Settings.plugins[definedSettings.pluginName] as any; @@ -264,11 +268,11 @@ export function definePluginSettings<D extends SettingsDefinition, C extends Set settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) as UseSettings<Settings>[] ).plugins[definedSettings.pluginName] as any, def, - checks: checks ?? {}, + checks: checks ?? {} as any, pluginName: "", - withPrivateSettings<T>() { - return this as DefinedSettings<D, C> & { store: T; }; + withPrivateSettings<T extends object>() { + return this as DefinedSettings<Def, Checks, T>; } }; diff --git a/src/utils/types.ts b/src/utils/types.ts index 6af1bdc..7b682e9 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -260,25 +260,29 @@ type SettingsStore<D extends SettingsDefinition> = { }; /** An instance of defined plugin settings */ -export interface DefinedSettings<D extends SettingsDefinition = SettingsDefinition, C extends SettingsChecks<D> = {}> { +export interface DefinedSettings< + Def extends SettingsDefinition = SettingsDefinition, + Checks extends SettingsChecks<Def> = {}, + PrivateSettings extends object = {} +> { /** Shorthand for `Vencord.Settings.plugins.PluginName`, but with typings */ - store: SettingsStore<D>; + store: SettingsStore<Def> & PrivateSettings; /** * React hook for getting the settings for this plugin * @param filter optional filter to avoid rerenders for irrelavent settings */ - use<F extends Extract<keyof D, string>>(filter?: F[]): Pick<SettingsStore<D>, F>; + use<F extends Extract<keyof Def | keyof PrivateSettings, string>>(filter?: F[]): Pick<SettingsStore<Def> & PrivateSettings, F>; /** Definitions of each setting */ - def: D; + def: Def; /** Setting methods with return values that could rely on other settings */ - checks: C; + checks: Checks; /** * Name of the plugin these settings belong to, * will be an empty string until plugin is initialized */ pluginName: string; - withPrivateSettings<T>(): this & { store: T; }; + withPrivateSettings<T extends object>(): DefinedSettings<Def, Checks, T>; } export type PartialExcept<T, R extends keyof T> = Partial<T> & Required<Pick<T, R>>; |