-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterator result of Iterable or AsyncIterable inferred to any #33932
Comments
With explicit check declare let syncIterable: Iterable<number>;
let res = syncIterable[Symbol.iterator]().next();
if (!res.done) {
res.value // number
} else {
res.value // any
} |
In addition to also wanting the fix suggested above (plumbing TReturn and TNext from AsyncIterableIterator to IteratorResult), I wonder if |
This is a duplicate of 33353. Also, your expected behavior is incorrect. Without checking const numbers: number[] = [];
const value = numers[Symbol.iterator]().next().value;
console.log(value); // undefined While the correct result should be |
Duplicate of #33353 |
TypeScript Version: 3.7.0-dev.20191010
Search Terms:
Iterable, AsyncIterable, Iterator, IteratorResult
Code
Synchronous iterators:
Asynchronous iterator:
Expected behavior:
In both cases
value
hasnumber
type.Actual behavior:
Variable
value
hasany
type.Investigation
Type
IteratorResult
has two type parametersT
andTReturn
:TypeScript/lib/lib.es2015.iterable.d.ts
Line 41 in 6162001
Because by default
TReturn
isany
(interfaceIterable
has only one type parameter), we gotnumber | any
type, which is narrowed toany
.There are two problems:
TReturn
type parameter forIterableIterator
andAsyncIterableIterator
types.Playground Link:
https://www.typescriptlang.org/play/index.html?ssl=3&ssc=23&pln=1&pc=1#code/CYUwxgNghgTiAEEQBd4GcCeA7MBJZIMUARkgFzz6ElIA8WArgLbGEB8A3AFBKpxrwAvOmx4CRUiADaAZQwsA9hAB0AS3FRkCmAF0AFAEplWEAA9kh7r3gA3KBAYJh-ZXYcgOQA
Related Issues:
#2983
The text was updated successfully, but these errors were encountered: