Skip to content

Commit

Permalink
Fix handling of generic functions in the types
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
sindresorhus committed Nov 17, 2023
1 parent 598642e commit 329a240
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export class AbortError extends Error {
private constructor();
}

export type ThrottledFunction<Argument extends readonly unknown[], ReturnValue> = ((...arguments: Argument) => Awaited<ReturnValue>) & {
type AnyFunction = (...arguments_: readonly any[]) => unknown;

export type ThrottledFunction<F extends AnyFunction> = F & {
/**
Whether future function calls should be throttled or count towards throttling thresholds.
Expand Down Expand Up @@ -78,4 +80,4 @@ for (let index = 1; index <= 6; index++) {
//=> 6: 2s
```
*/
export default function pThrottle(options: Options): <Argument extends readonly unknown[], ReturnValue>(function_: (...arguments: Argument) => ReturnValue) => ThrottledFunction<Argument, ReturnValue>;
export default function pThrottle(options: Options): <F extends AnyFunction>(function_: F) => ThrottledFunction<F>;
34 changes: 15 additions & 19 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,30 @@ const throttledLazyUnicorn = pThrottle({
interval: 1000,
})(async (_index: string) => '🦄');

const strictThrottledUnicorn = pThrottle({
limit: 1,
interval: 1000,
strict: true,
})((_index: string) => '🦄');

const strictThrottledLazyUnicorn = pThrottle({
limit: 1,
interval: 1000,
strict: true,
})(async (_index: string) => '🦄');

const throttledTaggedUnicorn = pThrottle({
limit: 1,
interval: 1000,
})((_index: number, tag: string) => `${tag}: 🦄`);

expectType<ThrottledFunction<[string], string>>(throttledUnicorn);
expectType<ThrottledFunction<[string], Promise<string>>>(throttledLazyUnicorn);
expectType<ThrottledFunction<[string], string>>(strictThrottledUnicorn);
expectType<ThrottledFunction<[string], Promise<string>>>(strictThrottledLazyUnicorn);
expectType<ThrottledFunction<[number, string], Promise<string>>>(throttledTaggedUnicorn);
expectType<string>(throttledUnicorn(''));
expectType<string>(await throttledLazyUnicorn(''));
expectType<string>(throttledTaggedUnicorn(1, 'foo'));

throttledUnicorn.abort();
throttledLazyUnicorn.abort();
strictThrottledUnicorn.abort();
strictThrottledLazyUnicorn.abort();
throttledTaggedUnicorn.abort();

expectType<boolean>(throttledUnicorn.isEnabled);
expectType<number>(throttledUnicorn.queueSize);

/* Generic */
declare function genericFunction<T>(argument: T): Promise<T>;

const throttledGenericFunction = pThrottle({
limit: 1,
interval: 1000,
})(genericFunction);

expectType<string>(await throttledGenericFunction('test'));
expectType<number>(await throttledGenericFunction(123));
/* /Generic */

0 comments on commit 329a240

Please sign in to comment.