如何将任何[] []分配给打字稿界面

时间:2018-12-11 00:30:38

标签: node.js typescript debugging types

我希望大家都过得不错。我是打字稿新手,正在尝试建立搜索功能,但遇到复杂类型的问题。在代码中,我调用了一个函数,该函数带有一个具有类型的参数,该参数被定义为接口。我可以简单地用正确的类型初始化一个对象,然后直接将其放入,错误消失,但是,我似乎无法在其上调用任何函数来返回类型为any [] []的对象。我正在使用过滤器方法尝试删除缺少的搜索参数。鉴于项目的结构,我也可以使用打字稿进行操作。

这里是代码的link ...

当您将鼠标悬停在红色弯曲的线条上时,您会看到错误。我不确定如何解决它,或者我是否可以为此完全解决它。这是错误的文字:

Type 'any[][]' is not assignable to type 'SearchParams'.
  Types of property '[Symbol.iterator]' are incompatible.
    Type '() => IterableIterator<any[]>' is not assignable to type '() => IterableIterator<[SearchParamTypes, string]>'.
      Type 'IterableIterator<any[]>' is not assignable to type 'IterableIterator<[SearchParamTypes, string]>'.
        Type 'any[]' is missing the following properties from type '[SearchParamTypes, string]': 0, 1

这也是一个代码段...(它没有运行。检查上面链接中的代码以查看错误。)

enum SearchParamTypes {
  Value = 'value',
  OptionalValue = 'optional'
  //...
}

interface SearchParams {
  [Symbol.iterator](): IterableIterator<[SearchParamTypes, string]>;
}

const cleanedParams : SearchParams = [
      [SearchParamTypes.Value,myValue],
      [SearchParamTypes.OptionalValue,myOptionalValue]
  ].filter(([,value])=> value);

这里有一个类似的问题得到了回答。 Property '0' is missing in type。 但是我无法为自己应用该解决方案。

也谢谢您。

0 个答案:

没有答案