From cab72e1be627fb7cb06725d0a3e262e214de170f Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sat, 4 Mar 2023 18:40:37 +0100 Subject: Strongly type useSettings (supersedes #559) --- src/api/settings.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/api') 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[]) { const [, forceUpdate] = React.useReducer(() => ({}), {}); const onUpdate: SubscriptionCallback = paths - ? (value, path) => paths.includes(path) && forceUpdate() + ? (value, path) => paths.includes(path as UseSettings) && forceUpdate() : forceUpdate; React.useEffect(() => { @@ -229,7 +229,7 @@ export function definePluginSettings useSettings( - settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) + settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) as UseSettings[] ).plugins[definedSettings.pluginName] as any, def, checks: checks ?? {}, @@ -237,3 +237,15 @@ export function definePluginSettings = ResolveUseSettings[keyof T]; + +type ResolveUseSettings = { + [Key in keyof T]: + Key extends string + ? T[Key] extends Record + // @ts-ignore "Type instantiation is excessively deep and possibly infinite" + ? UseSettings extends string ? `${Key}.${UseSettings}` : never + : Key + : never; +}; -- cgit