Title: Incorrect typing for iterables in lib · Issue #29982 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect typing for iterables in lib · Issue #29982 · microsoft/TypeScript
X Title: Incorrect typing for iterables in lib · Issue #29982 · microsoft/TypeScript
Description: Search Terms: iterable return type asynciterable return type Related Issues: #2983 - describes how the iterables types are not inferred correctly for generators. Fixing this current issue would be a step towards making that issue resolva...
Open Graph Description: Search Terms: iterable return type asynciterable return type Related Issues: #2983 - describes how the iterables types are not inferred correctly for generators. Fixing this current issue would be ...
X Description: Search Terms: iterable return type asynciterable return type Related Issues: #2983 - describes how the iterables types are not inferred correctly for generators. Fixing this current issue would be ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/29982
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect typing for iterables in lib","articleBody":"**Search Terms:**\r\n\r\niterable return type\r\nasynciterable return type\r\n\r\n**Related Issues:**\r\n\r\nhttps://github.com/Microsoft/TypeScript/issues/2983 - describes how the iterables types are not inferred correctly for generators. Fixing this current issue would be a step towards making that issue resolvable as well.\r\n\r\n**The Problem**\r\n\r\nThe type definitions for iterable and asynciterable do not match the specification.\r\n\r\nAccording to the specification, when `IteratorResult` has `done: true`, the `value` is the return value of the iterator. This is not the same type as an element of the iterator. When implementing an iterator or async iterator \"by hand\", you get spurious type errors. For example:\r\n\r\n```\r\nconst counter = (offset = 0, limit = 10) => {\r\n let x = 0;\r\n const a: AsyncIterator\u003cnumber> = {\r\n next: async () => {\r\n if (x \u003c 5) {\r\n x++;\r\n return { x, done: false };\r\n }\r\n return { done: true };\r\n },\r\n };\r\n};\r\n\r\nexport default counter;\r\n```\r\n\r\nGives errors:\r\n\r\n```\r\nsrc/testing/temp.ts:4:5 - error TS2322: Type '() => Promise\u003c{ x: number; done: false; } | { done: true; x?: undefined; }>' is not assignable to type '(value?: any) => Promise\u003cIteratorResult\u003cnumber>>'.\r\n Type 'Promise\u003c{ x: number; done: false; } | { done: true; x?: undefined; }>' is not assignable to type 'Promise\u003cIteratorResult\u003cnumber>>'.\r\n Type '{ x: number; done: false; } | { done: true; x?: undefined; }' is not assignable to type 'IteratorResult\u003cnumber>'.\r\n Property 'value' is missing in type '{ x: number; done: false; }' but required in type 'IteratorResult\u003cnumber>'.\r\n\r\n4 next: async () => {\r\n ~~~~\r\n\r\n```\r\n\r\nThus, the types for iterables and async iterables should look more like this:\r\n\r\n```\r\ninterface IteratorValueResult\u003cT> {\r\n done?: false;\r\n value: T;\r\n}\r\ninterface IteratorReturnResult\u003cT = undefined> {\r\n done: true;\r\n value: T;\r\n}\r\ntype IteratorResult\u003cT, U = undefined> = IteratorValueResult\u003cT> | IteratorReturnResult\u003cU>;\r\n\r\ninterface Iterator\u003cT, U = undefined> {\r\n next(value?: any): IteratorResult\u003cT, U>;\r\n return?(value?: any): IteratorResult\u003cT, U>;\r\n throw?(e?: any): IteratorResult\u003cT, U>;\r\n}\r\n\r\ninterface Iterable\u003cT, U = undefined> {\r\n [Symbol.iterator](): Iterator\u003cT, U>;\r\n}\r\n\r\ninterface IterableIterator\u003cT, U = undefined> extends Iterator\u003cT, U> {\r\n [Symbol.iterator](): IterableIterator\u003cT, U>;\r\n}\r\n```\r\n\r\nand \r\n\r\n```\r\ninterface AsyncIterator\u003cT, U = undefined, NextArg = undefined> {\r\n // the type of the argument to next (if any) depends on the specific async iterator\r\n next(value?: NextArg): Promise\u003cIteratorResult\u003cT, U>>;\r\n // return should give an iterator result with done: true` and the passed value (if any) as the value\r\n return?\u003cR = undefined>(value: R): Promise\u003cIteratorReturnResult\u003cR>>;\r\n // throw should return a rejected promise with the given argument as the error, or return an iterator result with done: true\r\n throw?(e: any): Promise\u003cIteratorReturnResult\u003cundefined>>;\r\n}\r\n\r\ninterface AsyncIterable\u003cT, U = undefined, NextArg = undefined> {\r\n [Symbol.asyncIterator](): AsyncIterator\u003cT, U, NextArg>;\r\n}\r\n\r\ninterface AsyncIterableIterator\u003cT, U = undefined, NextArg = undefined> extends AsyncIterator\u003cT, U> {\r\n [Symbol.asyncIterator](): AsyncIterableIterator\u003cT, U, NextArg>;\r\n}\r\n```\r\n\r\nThis should allow typescript to correctly infer that `value` is not required when `done: true`. For generators that do have a distinct return type from their yield type, they will be able to typecheck without spurious errors.\r\n\r\nIf the above definitions are acceptable to the maintainers I can prepare a PR to update the type definitions.\r\n\r\n**References**\r\n\r\n- [Iterator Interface](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-iterator-interface)\r\n- [AsyncIterator Interface](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-asynciterator-interface)\r\n- [IteratorResult Interface](https://www.ecma-international.org/ecma-262/9.0/index.html#sec-iteratorresult-interface)\r\n","author":{"url":"https://github.com/dobesv","@type":"Person","name":"dobesv"},"datePublished":"2019-02-20T00:28:24Z","url":"https://github.com/microsoft/TypeScript/issues/29982"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:22f63b6d-056b-26a9-2d88-20aecb00132e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B76E:142787:4C685E:697471:6B0453D3 |
| html-safe-nonce | a1ee8469cb0dd3a21d72c5512aec433625fcefc050337f87eba9b048373bc61e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzZFOjE0Mjc4Nzo0QzY4NUU6Njk3NDcxOjZCMDQ1M0QzIiwidmlzaXRvcl9pZCI6IjU3NDM2NzQ2ODY0MzE3NzQyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 78334fe457ebd4e4b0015ead6a296b0f7397b138941bc4e0aef684149c92ddf0 |
| hovercard-subject-tag | issue:412188044 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/29982/issue_layout |
| twitter:image | https://opengraph.githubassets.com/704d130572b1b078fab48e7aa2b1bfdb95b519cbd82336cd1f0996af8034feb5/microsoft/TypeScript/issues/29982 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/704d130572b1b078fab48e7aa2b1bfdb95b519cbd82336cd1f0996af8034feb5/microsoft/TypeScript/issues/29982 |
| og:image:alt | Search Terms: iterable return type asynciterable return type Related Issues: #2983 - describes how the iterables types are not inferred correctly for generators. Fixing this current issue would be ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dobesv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width