如何在 Typescript 中键入 returns 2 个或多个字符串数组的交集的递归函数?

How to type a recursive function that returns the intersection of 2 or more arrays of strings in Typescript?

这是我用来 return 2 个或更多字符串数组之间的交集的代码。

Typescript playground

export const intersection = (list1: string[], list2: string[], ...otherLists): string[] => {

  const result = [];

  for (let i = 0; i < list1.length; i++) {
      const item1 = list1[i];
      let found = false;
      for (let j = 0; j < list2.length && !found; j++) {
          found = item1 === list2[j];
      }
      if (found === true) {
          result.push(item1);
      }
  }

  if (otherLists.length) {
    return intersection(result, otherLists.shift(), ...otherLists);
  }

  else {
    return result;
  }
};

这在 JS 中工作正常。但是现在我正在将我的代码转换为 Typescript,我想知道如何输入 ...otherLists 参数。

我收到以下错误:

您可以这样输入:

export const intersection = (list1: string[], list2: string[], ...otherLists: string[][]): string[] => {

  const result = [];

  for (let i = 0; i < list1.length; i++) {
      const item1 = list1[i];
      let found = false;
      for (let j = 0; j < list2.length && !found; j++) {
          found = item1 === list2[j];
      }
      if (found === true) {
          result.push(item1);
      }
  }

  const firstArray = otherLists.shift();
  if (firstArray) {
    return intersection(result, firstArray, ...otherLists);
  }

  return result;
};