diff --git a/types/throttle-debounce/index.d.ts b/types/throttle-debounce/index.d.ts index 34bfbbdb5b..5b1f197a31 100644 --- a/types/throttle-debounce/index.d.ts +++ b/types/throttle-debounce/index.d.ts @@ -1,8 +1,17 @@ // Type definitions for throttle-debounce 1.1 // Project: https://github.com/niksy/throttle-debounce -// Definitions by: Marek Buchar , Frank Li +// Definitions by: Marek Buchar , Frank Li , Thomas Oddsund // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +export {}; + +interface Cancel { + cancel: () => void; +} + +export type throttle = T & Cancel; +export type debounce = throttle; + /** * Throttle execution of a function. Especially useful for rate limiting * execution of handlers on events like resize and scroll. @@ -34,8 +43,8 @@ export function throttle any>( delay: number, noTrailing: boolean, callback: T, - debounceMode?: boolean, -): T; + debounceMode?: boolean +): throttle; /** * Throttle execution of a function. Especially useful for rate limiting @@ -60,8 +69,8 @@ export function throttle any>( export function throttle any>( delay: number, callback: T, - debounceMode?: boolean, -): T; + debounceMode?: boolean +): throttle; /** * Debounce execution of a function. Debouncing, unlike throttling, @@ -87,7 +96,11 @@ export function throttle any>( * @return * A new, debounced function. */ -export function debounce any>(delay: number, atBegin: boolean, callback: T): T; +export function debounce any>( + delay: number, + atBegin: boolean, + callback: T +): debounce; /** * Debounce execution of a function. Debouncing, unlike throttling, @@ -106,4 +119,7 @@ export function debounce any>(delay: number, atBeg * @return * A new, debounced function. */ -export function debounce any>(delay: number, callback: T): T; +export function debounce any>( + delay: number, + callback: T +): debounce; diff --git a/types/throttle-debounce/throttle-debounce-tests.ts b/types/throttle-debounce/throttle-debounce-tests.ts index 0090eb1039..ff4f757dbc 100644 --- a/types/throttle-debounce/throttle-debounce-tests.ts +++ b/types/throttle-debounce/throttle-debounce-tests.ts @@ -1,15 +1,30 @@ -import throttleDebounce = require('throttle-debounce'); - -const { throttle, debounce } = throttleDebounce; +import { throttle, debounce } from "throttle-debounce"; type Func = () => void; const func: Func = () => {}; -const throttle1: Func = throttle(42, true, func, true); -const throttle2: Func = throttle(42, true, func); -const throttle3: Func = throttle(42, func, true); -const throttle4: Func = throttle(42, func); +const throttleWithoutCancel1: Func = throttle(42, true, func, true); +const throttleWithoutCancel2: Func = throttle(42, true, func); +const throttleWithoutCancel3: Func = throttle(42, func, true); +const throttleWithoutCancel4: Func = throttle(42, func); -const debounce1: Func = debounce(42, true, func); -const debounce2: Func = debounce(42, func); +throttleWithoutCancel1(); +// $ExpectError +throttleWithoutCancel1.cancel(); +const throttleWithCancel1: throttle = throttle(42, true, func, true); +const throttleWithCancel2: throttle = throttle(42, true, func); +const throttleWithCancel3: throttle = throttle(42, func, true); +const throttleWithCancel4: throttle = throttle(42, func); +throttleWithCancel1(); +throttleWithCancel1.cancel(); + +const debounceWithoutCancel1: Func = debounce(42, true, func); +const debounceWithoutCancel2: Func = debounce(42, func); +debounceWithoutCancel1(); +// $ExpectError +debounceWithoutCancel1.cancel(); +const debounceWithCancel1: debounce = debounce(42, true, func); +const debounceWithCancel2: debounce = debounce(42, func); +debounceWithCancel1(); +debounceWithCancel1.cancel();