TypeScript spread complains about target signature

I have a function defined as follows:

function create(element1: number, ...otherElements: any[]) {
  // Do something
  return makeSomething(...otherElements)
}


function makeSomething(a: string, b: number, c: IElements) {
  // Do something
}

TypeScript complains about the parameters that I pass to makeSomething

error TS2346: the supplied parameters do not match any target call signature.

What is the correct way to define such a thing when using distribution syntax?

thank

+4
source share
1 answer

One problem is that Typescript does not know how many elements are in ...otherElements: any[]. It cannot guarantee that makeSomething3 elements will be sent, although the element type ( any) will be valid for the arguments.

, , :

function create(element1: number, ...otherElements: any[]) {
  // Do something
  const [a, b, c] = otherElements;
  return makeSomething(a, b, c)
}

, .., , .

, rest destructure:

function create(element1: number, ...otherElements: any[]) {
  // Do something
  const [a, b, c, ...d] = otherElements;
  return makeSomething(a, b, c, ...d)
}
+4

Source: https://habr.com/ru/post/1680233/


All Articles