diff options
author | Vendicated <vendicated@riseup.net> | 2023-03-04 18:40:37 +0100 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2023-03-04 18:41:32 +0100 |
commit | cab72e1be627fb7cb06725d0a3e262e214de170f (patch) | |
tree | dcd1daddf420be1bad26955d15ec4ea9ec65abfb /src/api | |
parent | 92372bde1d422e2daf2e24dd05ae5f4d73f5ebcb (diff) | |
download | Vencord-cab72e1be627fb7cb06725d0a3e262e214de170f.tar.gz Vencord-cab72e1be627fb7cb06725d0a3e262e214de170f.tar.bz2 Vencord-cab72e1be627fb7cb06725d0a3e262e214de170f.zip |
Strongly type useSettings (supersedes #559)
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/settings.ts | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/api/settings.ts b/src/api/settings.ts index c551df0..1ca2611 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -167,11 +167,11 @@ export const Settings = makeProxy(settings); * @returns Settings */ // TODO: Representing paths as essentially "string[].join('.')" wont allow dots in paths, change to "paths?: string[][]" later -export function useSettings(paths?: string[]) { +export function useSettings(paths?: UseSettings<Settings>[]) { const [, forceUpdate] = React.useReducer(() => ({}), {}); const onUpdate: SubscriptionCallback = paths - ? (value, path) => paths.includes(path) && forceUpdate() + ? (value, path) => paths.includes(path as UseSettings<Settings>) && forceUpdate() : forceUpdate; React.useEffect(() => { @@ -229,7 +229,7 @@ export function definePluginSettings<D extends SettingsDefinition, C extends Set return Settings.plugins[definedSettings.pluginName] as any; }, use: settings => useSettings( - settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) + settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) as UseSettings<Settings>[] ).plugins[definedSettings.pluginName] as any, def, checks: checks ?? {}, @@ -237,3 +237,15 @@ export function definePluginSettings<D extends SettingsDefinition, C extends Set }; return definedSettings; } + +type UseSettings<T extends object> = ResolveUseSettings<T>[keyof T]; + +type ResolveUseSettings<T extends object> = { + [Key in keyof T]: + Key extends string + ? T[Key] extends Record<string, unknown> + // @ts-ignore "Type instantiation is excessively deep and possibly infinite" + ? UseSettings<T[Key]> extends string ? `${Key}.${UseSettings<T[Key]>}` : never + : Key + : never; +}; |