0
function debouncePromise<TParams extends Array<unknown>, TRes>( fn: (a: TParams) => Promise<TRes>, time: number, ) { let timerId: ReturnType<typeof setTimeout> | undefined = undefined; return function debounced(...args: TParams) { if (timerId) { clearTimeout(timerId); } return new Promise((resolve) => { timerId = setTimeout(() => resolve(fn(...args)), time); //<= A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556) }); }; } 

I have already typed the spread argument in the function as a rest parameter. Why am I recieving an error on an argument/variable that has already been explicitly typed? How do I resolve this?

Update: tsplayground

3
  • 1
    Do you have a minimal reproducible example for this? (e.g. on the typescript playground or the like? And if you do, don't replace your code, just add the link to that in your post). But in the absence of that: fn was declared as taking 1 argument, of type TParams, on line 2. But on line 12 you're not passing it 1 argument of type TParams. Commented Mar 21, 2024 at 0:50
  • On line 2: fn: (a: TParams) => fn: (...a: TParams) Commented Mar 21, 2024 at 0:58
  • @Mike'Pomax'Kamermans ahhh i changed it to fn: (...a: TParams) => and that resolved it! thank you :) Commented Mar 21, 2024 at 1:02

1 Answer 1

0

Made a change in line 2 to fn: (...a: TParams) => and that resolved it!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.