diff --git a/types/debounce-fn/debounce-fn-tests.ts b/types/debounce-fn/debounce-fn-tests.ts new file mode 100644 index 0000000000..9f9d3881e9 --- /dev/null +++ b/types/debounce-fn/debounce-fn-tests.ts @@ -0,0 +1,10 @@ +import debounceFn = require('debounce-fn'); + +const debounced = debounceFn((s: string) => true); +debounced; // $ExpectType ((s: string) => boolean | undefined) & { cancel(): void; } + +debounceFn((s: string) => true); +debounceFn((s: string) => true, { wait: 100 }); +debounceFn((s: string) => true, { immediate: true }); + +debounced.cancel(); diff --git a/types/debounce-fn/index.d.ts b/types/debounce-fn/index.d.ts new file mode 100644 index 0000000000..01e147a14a --- /dev/null +++ b/types/debounce-fn/index.d.ts @@ -0,0 +1,34 @@ +// Type definitions for debounce-fn 1.0 +// Project: https://github.com/sindresorhus/debounce-fn#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +export = debounce; + +/** + * Returns a debounced function that delays calling the `input` function until after `wait` milliseconds + * have elapsed since the last time the debounced function was called. + * + * It comes with a `.cancel()` method to cancel any scheduled `input` function calls. + */ +declare function debounce( + input: (...args: TArgs) => TResult, + options?: debounce.Options +): ((...args: TArgs) => TResult | undefined) & { cancel(): void }; + +declare namespace debounce { + interface Options { + /** + * Time to wait until the `input` function is called. + * @default 0 + */ + wait?: number; + /** + * Trigger the function on the leading edge instead of the trailing edge of the `wait` interval. + * For example, can be useful for preventing accidental double-clicks on a "submit" button + * from firing a second time. + */ + immediate?: boolean; + } +} diff --git a/types/debounce-fn/tsconfig.json b/types/debounce-fn/tsconfig.json new file mode 100644 index 0000000000..f2d90018fb --- /dev/null +++ b/types/debounce-fn/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "debounce-fn-tests.ts" + ] +} diff --git a/types/debounce-fn/tslint.json b/types/debounce-fn/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/debounce-fn/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }