为什么TypeScript 2.0更改了IteratorResult<;K>;?
从TypeScript 1.8切换到2.0.3时,一些实现迭代器的代码已开始生成新消息:错误TS2322:类型“{done:true;}”不可分配给类型“IteratorResult”。类型“{done:true;}”中缺少属性“value”。虽然修复很容易(并且向后兼容),但我想了解它为什么会更改 至少在TypeScript 1.8中,为什么TypeScript 2.0更改了IteratorResult<;K>;?,typescript,Typescript,从TypeScript 1.8切换到2.0.3时,一些实现迭代器的代码已开始生成新消息:错误TS2322:类型“{done:true;}”不可分配给类型“IteratorResult”。类型“{done:true;}”中缺少属性“value”。虽然修复很容易(并且向后兼容),但我想了解它为什么会更改 至少在TypeScript 1.8中,IteratorResult是用值属性(可选)定义的。从1.8的lib.es6.d.ts: interface IteratorResult<T>
IteratorResult
是用值
属性(可选)定义的。从1.8的lib.es6.d.ts:
interface IteratorResult<T> {
done: boolean;
value?: T;
}
以下是生成错误的代码示例,以供参考:
class HashMapKeyIterable<K,V> implements Iterator<K>, IterableIterator<K> {
private _bucket: HashMapEntry<K,V>[];
private _index: number;
constructor( private _buckets : Iterator<HashMapEntry<K,V>[]> ){
this._bucket = undefined;
this._index = undefined;
}
[Symbol.iterator]() { return this }
next(): IteratorResult<K> {
while (true) {
if (this._bucket) {
const i = this._index++;
if (i < this._bucket.length) {
let item = this._bucket[i];
return {done: false, value: item.key}
}
}
this._index = 0
let x = this._buckets.next();
if (x.done) return {done: true}; // Under TS 2.0 this needs to
this._bucket = x.value; // return {done: true: value: undefined};
}
}
}
类HashMapKeyIterable实现迭代器IterableIterator{
私有_bucket:HashMapEntry[];
私有索引:数字;
构造函数(私有_bucket:迭代器){
此._bucket=未定义;
此._索引=未定义;
}
[Symbol.iterator](){返回此}
next():IteratorResult{
while(true){
如果(这个桶){
常数i=这个;
如果(i
更新:这现在看起来像一个bug,我已经提交了问题跟踪
return { done: true, value: undefined } as any as IteratorResult<T>;
class HashMapKeyIterable<K,V> implements Iterator<K>, IterableIterator<K> {
private _bucket: HashMapEntry<K,V>[];
private _index: number;
constructor( private _buckets : Iterator<HashMapEntry<K,V>[]> ){
this._bucket = undefined;
this._index = undefined;
}
[Symbol.iterator]() { return this }
next(): IteratorResult<K> {
while (true) {
if (this._bucket) {
const i = this._index++;
if (i < this._bucket.length) {
let item = this._bucket[i];
return {done: false, value: item.key}
}
}
this._index = 0
let x = this._buckets.next();
if (x.done) return {done: true}; // Under TS 2.0 this needs to
this._bucket = x.value; // return {done: true: value: undefined};
}
}
}