From 7b71f71f1044cea39d325b0d20bdecf88b22fec6 Mon Sep 17 00:00:00 2001 From: Mayank Kumar Chaudhari Date: Tue, 30 Apr 2024 20:04:32 +0530 Subject: [PATCH] Fix types --- lib/r18gs/src/with-plugins.ts | 22 +++++++++++++++------- lib/r18gs/tests/create.test.tsx | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/r18gs/src/with-plugins.ts b/lib/r18gs/src/with-plugins.ts index d877ce43..70a2db46 100644 --- a/lib/r18gs/src/with-plugins.ts +++ b/lib/r18gs/src/with-plugins.ts @@ -18,7 +18,7 @@ import type { Plugin, SetStateAction } from "./utils"; * @param key - Unique key to identify the store. * @param value - Initial value of the store. * @param plugins - Plugins to be applied to the store. - * @returns - A hook funciton that returns a tuple (Ordered sequance of values) containing the state and a function to set the state. + * @returns - A hook function that returns a tuple (Ordered sequence of values) containing the state and a function to set the state. */ export function create( key: string, @@ -35,16 +35,24 @@ export function create( * @returns A hook that automatically initializes the store (if not already initialized) with the given plugins. */ export function withPlugins( - plugins?: Plugin[], -): (key: string, value?: T, doNotInit?: boolean) => [T, SetStateAction] { + plugins: Plugin[], +): (key: string, value?: U, doNotInit?: boolean) => [U, SetStateAction] { /** - * todo - this typedoc comments are not visible in IDE suggestions - fix this + * Creates a hook similar to useRGS, with plugins applied on first invocation. + * * @param key - Unique key to identify the store. * @param value - Initial value of the store. - * @param doNotInit - @defaultValue false - Do not initialize the store. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized. + * @param doNotInit - If true, the store won't be initialized immediately. Defaults to false. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized. + * @returns A tuple containing the state value and its setter function. */ - return (key: string, value?: T, doNotInit = false) => - useRGSWithPlugins(key, value, plugins, doNotInit); + const hookWithPlugins = ( + key: string, + value?: U, + doNotInit = false, + ): [U, SetStateAction] => + useRGSWithPlugins(key, value, plugins as unknown as Plugin[], doNotInit); + + return hookWithPlugins; } export { useRGSWithPlugins }; diff --git a/lib/r18gs/tests/create.test.tsx b/lib/r18gs/tests/create.test.tsx index f5f1e09f..cfa5d77e 100644 --- a/lib/r18gs/tests/create.test.tsx +++ b/lib/r18gs/tests/create.test.tsx @@ -14,7 +14,7 @@ const TESTID_DISPLAY2 = "d2"; const useMyRGS = create(COUNTER_RGS_KEY, 0, [persist({ storage: "cookie" })]); -const useMyRGS2 = withPlugins([persist()]); +const useMyRGS2 = withPlugins([persist()]); function Component1() { const [count, setCount] = useMyRGS();