Why did TypeScript 2.0 change the IteratorResult <K>?

Switch from TypeScript from 1.8 to 2.0.3. Some code that implements Iterator began to generate a new message: error TS2322: Type '{done: true; } 'is not assigned to the type' IteratorResult '. The 'value' property is missing in the type '{done: true; } '. Although the fix is ​​simple (and backward compatible), I would like to understand why it has changed ...

At least in TypeScript 1.8, it IteratorResultwas valueoptionally defined with a parameter . From lib.es6.d.ts for 1.8:

interface IteratorResult<T> {
    done: boolean;
    value?: T;
}

In section 2.0, the declaration states:

interface IteratorResult<T> {
    done: boolean;
    value: T;
}

With a strict zero check off, the fix is { done: true, value: undefined }obvious, but is there any good reason to make it valuemandatory in 2.0?

UPDATE: , , , undefined ( ) . :

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};
            }
        }
    }

: , 11375 .

+4

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


All Articles