This package provides a robust TypeScript implementation of the debounce function, designed to limit the rate at which a function can be called. It is currently at version 5.0.0 and sees releases driven by feature enhancements and necessary fixes, rather than a strict schedule. Key features include full TypeScript support with improved type inference, cancellation functionality, an optional `maxWait` parameter to force execution after a maximum delay, and comprehensive Promise integration, allowing debouncing of promise-returning functions and returning promises from debounced calls. It differentiates itself by being TypeScript-first, directly addressing common debounce use cases with strong typing, and offering flexibility similar to popular utility libraries like Lodash but with a smaller footprint and modern ESM support.
npm install ts-debounceVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic function debouncing for a synchronous logger and advanced usage involving asynchronous functions with Promise support and the cancellation mechanism.
Review and update type annotations for functions debounced in version 5.0.0, especially those returning promises, to align with the stricter `Awaited` inference. Adjust expectations for `Promise` return types to `Promise<T>` instead of `Promise<Promise<T>>`.
Update your function signatures and event handlers to correctly type the arguments based on the improved inference (e.g., `(event: Event)` instead of `(event: any)`). This generally leads to more robust and safer code.
Ensure that your target execution environment provides a global `Promise` implementation, or include a polyfill (e.g., `core-js`) if targeting older environments.
Carefully consider the interaction of `isImmediate` with `waitMilliseconds`. If you always want a delay before the first execution, do not set `isImmediate` to `true`. Read the documentation thoroughly to understand its behavior.
Update the type signature of your debounced function's arguments to match the expected specific type (e.g., `(event: Event)`). For example, `debounce((event: Event) => { /* ... */ }, 300);`Ensure you are destructuring the named export: `const { debounce } = require('ts-debounce');`. If using ES Modules, prefer `import { debounce } from 'ts-debounce';`.Include a `Promise` polyfill (e.g., from `core-js` or `es6-promise`) in your application bundle to provide the necessary global `Promise` implementation.
No dependency data recorded yet.