Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TS2322和TS2495-具有ES6和ES5目标的迭代器_Javascript_Typescript_Ecmascript 6 - Fatal编程技术网

Javascript TS2322和TS2495-具有ES6和ES5目标的迭代器

Javascript TS2322和TS2495-具有ES6和ES5目标的迭代器,javascript,typescript,ecmascript-6,Javascript,Typescript,Ecmascript 6,对于下面的代码 class MakeIterable { index: number; data: number[]; constructor(data: number[]) { this.index = 0; this.data = data; } [Symbol.iterator]() { return { next: () => { if (this.index &l

对于下面的代码

class MakeIterable {

    index: number;
    data: number[];

    constructor(data: number[]) {
      this.index = 0;
      this.data = data;
    }

    [Symbol.iterator]() {
      return {
        next: () => {
          if (this.index < this.data.length) {
            return {
                value: this.data[this.index++], 
                done: false
            };
          } else {
            this.index = 0; //If we would like to iterate over this again without forcing manual update of the index
            return {done: true};
          }
        }
      }
    };
}

const itrble: MakeIterable = new MakeIterable([1,2,3,4,5]);

for (const val of itrble) {
    console.log(val);  // expecting '1' '2' '3' '4' '5' 
}
}

如何解决以下错误

$ tsc --target ES5
tstut.ts(30,19): error TS2495: Type 'MakeIterable' is not an array type or a string type

$ tsc --target ES6
tstut.ts(30,19): error TS2322: Type 'MakeIterable' is not assignable to type 'Iterable<number>'.
  Types of property '[Symbol.iterator]' are incompatible.
    Type '() => { next: () => { value: number; done: boolean; } | { done: boolean; value?: undefined; }; }' is not assignable to type '() => Iterator<number>'.
      Type '{ next: () => { value: number; done: boolean; } | { done: boolean; value?: undefined; }; }' is not assignable to type 'Iterator<number>'.
        Types of property 'next' are incompatible.
          Type '() => { value: number; done: boolean; } | { done: boolean; value?: undefined; }' is not assignable to type '(value?: any) => IteratorResult<number>'.
            Type '{ value: number; done: boolean; } | { done: boolean; value?: undefined; }' is not assignable to type 'IteratorResult<number>'.
              Type '{ done: boolean; value?: undefined; }' is not assignable to type 'IteratorResult<number>'.
                Property 'value' is optional in type '{ done: boolean; value?: undefined; }' but required in type 'IteratorResult<number>'.
TS2495:类型“MakeIterable”不是数组类型或字符串类型

使用了来自该非数组迭代器的结果

由于历史原因,of和其他迭代方法将iterables视为数组,这会导致不兼容但更简洁的输出

应启用该选项以正确处理非数组可重用项

这对于ES6目标不是必需的。它应该按照预期工作,而不需要向下迭代

TS2322:类型“MakeIterable”不可分配给类型“Iterable”

next并不总是返回值,但它应该返回。可能应该是:

      ...
      } else {
        return {value: undefined, done: true};
      }
      ...

关于错误消息,您不了解什么?很明显,您返回的迭代器结果应该总是有一个值property@bergi但是,如果它是完全有效的js,为什么在typescript中是无效的呢?@JonasW。因为JS没有typechecker?在发布之前没有看到Bergi的评论,但是是的,这是错误的第二部分的问题。实际上,这在TS 2.0中发生了变化,请看是不是非数组,因为实例有成员、索引和数据?它不是数组,因为它是迭代器,而不是数组。它具有Symbol.iterator属性,但没有长度和数字索引属性。顺便说一句,您不应该执行index=0。始终创建一个新的迭代器。See['a','b','c']应该是可Iterable的,因为它必须具有属性名[Symbol.iterator]和值零参数函数,该函数返回符合迭代器协议的对象。。迭代器协议定义了生成值序列的标准方法。['a'、'b'、'c']在内部应遵循该协议。我没有理解,当你说,MakeIterable是非数组迭代器。为什么Iterable/迭代器项与数组/字符串对齐?要使MakeIterable看起来像数组迭代器,需要做哪些更改?如果没有DownlevelIterable,iterable应该公开长度0,1。。。道具这被称为类似数组的对象,以使其在运行时工作,因为TS使用[].slice.calliterator技巧使其在ES5中可使用这就是不兼容但更简洁的输出。它应该包含使其可编译的代码,因为这是打字系统所需要的。您可以检查编译器ES5的输出,有无downlevelIteration,以了解一些情况。
      ...
      } else {
        return {value: undefined, done: true};
      }
      ...