Skip to content

refactor(core): narrow types in debounce utility#362

Open
guesung wants to merge 2 commits intotoss:mainfrom
guesung:refactor/core-debounce-narrow-types
Open

refactor(core): narrow types in debounce utility#362
guesung wants to merge 2 commits intotoss:mainfrom
guesung:refactor/core-debounce-narrow-types

Conversation

@guesung
Copy link
Copy Markdown
Contributor

@guesung guesung commented Apr 1, 2026

Summary

  • Narrow types in debounce.ts to reduce any usage
    • pendingThis: anyThisParameterType<F> | undefined
    • debounced function this context: anyThisParameterType<F>
  • Keep 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)
  • Replace file-level eslint-disable with inline eslint-disable-next-line on the two lines that need it

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
Copilot AI review requested due to automatic review settings April 1, 2026 06:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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[] to unknown[].
  • Typed pendingThis and the debounced function’s this parameter using ThisParameterType<F>.
  • Removed the file-level eslint-disable @typescript-eslint/no-explicit-any comment.

Comment on lines 3 to 4
export type DebouncedFunction<F extends (...args: unknown[]) => void> = {
(...args: Parameters<F>): void;
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 19 to 23
export function debounce<F extends (...args: unknown[]) => void>(
func: F,
debounceMs: number,
{ edges = ['leading', 'trailing'] }: DebounceOptions = {}
): DebouncedFunction<F> {
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
…-disable

`any[]` is required for generic function constraints due to parameter
contravariance — `unknown[]` rejects callbacks with specific param types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants