From 18a9d6f7c8e0d4b687f1bb8b6a3478ab9beee0f0 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 16 Sep 2023 15:36:03 +0800 Subject: [PATCH] fix(#9): disallow debounce of a function --- src/use-debounced-value/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/use-debounced-value/index.ts b/src/use-debounced-value/index.ts index 3e91c7e2..3ab6e7ff 100644 --- a/src/use-debounced-value/index.ts +++ b/src/use-debounced-value/index.ts @@ -1,8 +1,15 @@ import 'client-only'; import { useEffect, useState, useRef } from 'react'; +// eslint-disable-next-line @typescript-eslint/ban-types -- explicitly baning ALL functions +type NotFunction = T extends Function ? never : T; + /** @see https://foxact.skk.moe/use-debounced-value */ -export function useDebouncedValue(value: T, wait: number, leading = false) { +export function useDebouncedValue(value: NotFunction, wait: number, leading = false) { + if (typeof value === 'function') { + throw new TypeError('useDebouncedValue does not support function as value'); + } + const [outputValue, setOutputValue] = useState(value); const leadingRef = useRef(true);