refactor(core): narrow types in debounce utility#362
refactor(core): narrow types in debounce utility#362
Conversation
Replace `any` with precise types in debounce.ts: - `any[]` → `unknown[]` in generic constraints - `any` → `ThisParameterType<F> | undefined` for pendingThis - `this: any` → `this: ThisParameterType<F>` in debounced function - Remove unnecessary eslint-disable comment
There was a problem hiding this comment.
Pull request overview
This PR refactors the core useDebounce utility’s internal debounce.ts typings to remove any usage and better model this context for debounced functions.
Changes:
- Narrowed the generic function constraint from
any[]tounknown[]. - Typed
pendingThisand the debounced function’sthisparameter usingThisParameterType<F>. - Removed the file-level
eslint-disable @typescript-eslint/no-explicit-anycomment.
| export type DebouncedFunction<F extends (...args: unknown[]) => void> = { | ||
| (...args: Parameters<F>): void; |
There was a problem hiding this comment.
The F extends (...args: unknown[]) => void constraint is too strict under strictFunctionTypes: a typical callback like (value: string) => void does not satisfy it (because string is not a supertype of unknown). This makes DebouncedFunction<typeof callback> unusable for most typed functions and will likely break downstream typechecking. Consider reverting the args constraint to any[] (with a localized eslint disable) or using a different “any function” helper type that still accepts specific parameter types.
| export function debounce<F extends (...args: unknown[]) => void>( | ||
| func: F, | ||
| debounceMs: number, | ||
| { edges = ['leading', 'trailing'] }: DebounceOptions = {} | ||
| ): DebouncedFunction<F> { |
There was a problem hiding this comment.
Same issue as the DebouncedFunction type: constraining debounce with F extends (...args: unknown[]) => void rejects most typed callbacks (e.g. (value: string) => void) in strict mode, which can break internal callers and public API usage. Please loosen this constraint so typed parameter lists are accepted.
…-disable `any[]` is required for generic function constraints due to parameter contravariance — `unknown[]` rejects callbacks with specific param types.
Summary
debounce.tsto reduceanyusagependingThis:any→ThisParameterType<F> | undefineddebouncedfunctionthiscontext:any→ThisParameterType<F>any[]in generic function constraint (F extends (...args: any[]) => void) — required due to parameter contravariance (unknown[]rejects callbacks with specific param types like(value: string) => void)eslint-disablewith inlineeslint-disable-next-lineon the two lines that need it