Skip to content

Commit

Permalink
fix(#9): disallow debounce of a function
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 16, 2023
1 parent 5f6ec49 commit 18a9d6f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/use-debounced-value/index.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends Function ? never : T;

/** @see https://foxact.skk.moe/use-debounced-value */
export function useDebouncedValue<T>(value: T, wait: number, leading = false) {
export function useDebouncedValue<T>(value: NotFunction<T>, 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);

Expand Down

0 comments on commit 18a9d6f

Please sign in to comment.