Skip to content

Commit

Permalink
Merge pull request #15 from cszhjh/fix-types
Browse files Browse the repository at this point in the history
fix: improve the type of parameter `fn`
  • Loading branch information
cszhjh authored Oct 30, 2024
2 parents 88daff6 + 07922bf commit 23a1670
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { isArray } from './general'

export function NOOP() {}

export function debounce(fn: any, delay = 0) {
export function debounce<F extends (...args: any[]) => any>(fn: F, delay = 0) {
let timer: any

return function (this: unknown, ...args: any[]) {
return function (this: unknown, ...args: Parameters<F>) {
if (timer) {
clearTimeout(timer)
}
Expand All @@ -16,11 +16,11 @@ export function debounce(fn: any, delay = 0) {
}
}

export function throttle(fn: any, delay = 200): () => void {
export function throttle<F extends (...args: any[]) => any>(fn: F, delay = 200) {
let timer: any
let start = 0

return function loop(this: unknown, ...args) {
return function loop(this: unknown, ...args: Parameters<F>) {
const now = performance.now()
const elapsed = now - start

Expand Down

0 comments on commit 23a1670

Please sign in to comment.