TypeScript Version: 3.6.2
Search Terms:
async iterator iterable next
Code:
filename: constant.ts
classConstantIterator<T>implementsAsyncIterator<T,undefined,T|undefined>{constructor(privateconstant: T){}asyncnext(value?: T): Promise<IteratorResult<T>>{if(value!=null){thrownewError("ConstantIterator.prototype.next may not take any values");}return{value: this.constant,done: false};}}Expected behavior:
Code compiles.
Actual behavior:
constant.ts(4,9): error TS2416: Property'next'intype 'ConstantIterator<T>' is not assignable to the same property in base type 'AsyncIterator<T,undefined,T>'.Type '(value?: T)=>Promise<{value: T; done: boolean;}>' is not assignable to type '(...args: []|[T|PromiseLike<T>])=>Promise<IteratorResult<T,undefined>>'.Typesofparameters 'value' and'args'areincompatible.Type'[] | [T | PromiseLike<T>]'isnotassignabletotype'[T?]'.Type'[T | PromiseLike<T>]'isnotassignabletotype'[T?]'.Typesofproperty'0'areincompatible.Type 'T|PromiseLike<T>' is not assignable to type 'T'. 'T|PromiseLike<T>' is assignable to the constraint of type 'T',but'T'couldbeinstantiatedwithadifferentsubtypeofconstraint'{}'.Type 'PromiseLike<T>' is not assignable to type 'T'. 'PromiseLike<T>' is assignable to the constraint of type 'T',but'T'couldbeinstantiatedwithadifferentsubtypeofconstraint'{}'.Note that the synchronous version of ConstantIterator does not throw the same error. It’s the type union of T | PromiseLike<T> which seems to cause the error.
Playground Link:
Playground has yet to be updated for 3.6.
Related Issues:
#30790 Strongly typed iterator PR.
#32682 Other (async) iterator usability issues.
#33131 Possible change to type inference in 3.6.
TypeScript Version: 3.6.2
Search Terms:
async iterator iterable next
Code:
filename: constant.ts
Expected behavior:
Code compiles.
Actual behavior:
Note that the synchronous version of
ConstantIteratordoes not throw the same error. It’s the type union ofT | PromiseLike<T>which seems to cause the error.Playground Link:
Playground has yet to be updated for 3.6.
Related Issues:
#30790 Strongly typed iterator PR.
#32682 Other (async) iterator usability issues.
#33131 Possible change to type inference in 3.6.