From 9cc0f88e02a169cd06f596c04a37979ee0398153 Mon Sep 17 00:00:00 2001 From: "Michael (mcm)" Date: Wed, 7 Dec 2022 09:32:53 +0100 Subject: [PATCH] [IMP] hooks: add types to useEffect to improve DX --- src/runtime/hooks.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/runtime/hooks.ts b/src/runtime/hooks.ts index 7fecd555..beac0c28 100644 --- a/src/runtime/hooks.ts +++ b/src/runtime/hooks.ts @@ -59,28 +59,35 @@ export function useChildSubEnv(envExtension: Env) { // useEffect // ----------------------------------------------------------------------------- +type EffectDeps = T | (T extends [...infer H, never] ? EffectDeps : never); + /** - * @param {...any} dependencies the dependencies computed by computeDependencies + * @template T + * @param {...T} dependencies the dependencies computed by computeDependencies * @returns {void|(()=>void)} a cleanup function that reverses the side * effects of the effect callback. */ -type Effect = (...dependencies: any[]) => void | (() => void); +type Effect = (...dependencies: EffectDeps) => void | (() => void); /** * This hook will run a callback when a component is mounted and patched, and * will run a cleanup function before patching and before unmounting the * the component. * - * @param {Effect} effect the effect to run on component mount and/or patch - * @param {()=>any[]} [computeDependencies=()=>[NaN]] a callback to compute + * @template T + * @param {Effect} effect the effect to run on component mount and/or patch + * @param {()=>T} [computeDependencies=()=>[NaN]] a callback to compute * dependencies that will decide if the effect needs to be cleaned up and * run again. If the dependencies did not change, the effect will not run * again. The default value returns an array containing only NaN because * NaN !== NaN, which will cause the effect to rerun on every patch. */ -export function useEffect(effect: Effect, computeDependencies: () => any[] = () => [NaN]) { +export function useEffect( + effect: Effect, + computeDependencies: () => T = () => [NaN] as never +) { let cleanup: (() => void) | void; - let dependencies: any[]; + let dependencies: T; onMounted(() => { dependencies = computeDependencies(); cleanup = effect(...dependencies);