René's URL Explorer Experiment


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

direct link

Domain: github.com

IPv4 address: 140.82.113.4


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:22f63b6d-056b-26a9-2d88-20aecb00132e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB76E:142787:4C685E:697471:6B0453D3
html-safe-noncea1ee8469cb0dd3a21d72c5512aec433625fcefc050337f87eba9b048373bc61e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzZFOjE0Mjc4Nzo0QzY4NUU6Njk3NDcxOjZCMDQ1M0QzIiwidmlzaXRvcl9pZCI6IjU3NDM2NzQ2ODY0MzE3NzQyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac78334fe457ebd4e4b0015ead6a296b0f7397b138941bc4e0aef684149c92ddf0
hovercard-subject-tagissue:412188044
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/29982/issue_layout
twitter:imagehttps://opengraph.githubassets.com/704d130572b1b078fab48e7aa2b1bfdb95b519cbd82336cd1f0996af8034feb5/microsoft/TypeScript/issues/29982
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/704d130572b1b078fab48e7aa2b1bfdb95b519cbd82336cd1f0996af8034feb5/microsoft/TypeScript/issues/29982
og:image:altSearch 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedobesv
hostnamegithub.com
expected-hostnamegithub.com
None910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasee086f9b7e88322eb2b41885a6ad425b215737fa5
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/29982#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F29982
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F29982
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/29982
Reloadhttps://github.com/microsoft/TypeScript/issues/29982
Reloadhttps://github.com/microsoft/TypeScript/issues/29982
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests320 (320)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/29982
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/29982
Incorrect typing for iterables in libhttps://github.com/microsoft/TypeScript/issues/29982#top
https://github.com/rbuckton
Needs InvestigationThis issue needs a team member to investigate its status.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Investigation%22
https://github.com/dobesv
Dobes Vandermeer (dobesv)https://github.com/dobesv
on Feb 20, 2019https://github.com/microsoft/TypeScript/issues/29982#issue-412188044
#2983https://github.com/microsoft/TypeScript/issues/2983
Iterator Interfacehttps://www.ecma-international.org/ecma-262/9.0/index.html#sec-iterator-interface
AsyncIterator Interfacehttps://www.ecma-international.org/ecma-262/9.0/index.html#sec-asynciterator-interface
IteratorResult Interfacehttps://www.ecma-international.org/ecma-262/9.0/index.html#sec-iteratorresult-interface
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Ron Buckton (rbuckton)https://github.com/rbuckton
on Feb 27, 2019https://github.com/microsoft/TypeScript/issues/29982#event-2166880693
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Needs More InfoThe issue still hasn't been fully clarifiedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20More%20Info%22
on Feb 27, 2019https://github.com/microsoft/TypeScript/issues/29982#event-2166880798
https://github.com/justjake
Jake Teton-Landis (justjake)https://github.com/justjake
on Apr 12, 2019https://github.com/microsoft/TypeScript/issues/29982#issuecomment-482700355
https://github.com/justjake/ts-3.4.-async-iterable-issuehttps://github.com/justjake/ts-3.4.-async-iterable-issue
https://github.com/Microsoft/TypeScript/issues/23030#issuecomment-482692170https://github.com/Microsoft/TypeScript/issues/23030#issuecomment-482692170
https://www.typescriptlang.org/play/#src=async%20function*%20generate()%20%7B%0D%0A%20%20yield%201%0D%0A%20%20yield%202%0D%0A%20%20yield%203%0D%0A%7D%0D%0A%0D%0A%2F%2F%20AsyncIterableIterator%3C1%20%7C%202%20%7C%203%3E%0D%0Atype%20Out%20%3D%20ReturnType%3Ctypeof%20generate%3E%0D%0A%0D%0Aasync%20function%20consume()%20%7B%0D%0A%20%20const%20iterator%20%3D%20generate()%0D%0A%20%20const%20yield1%20%3D%20await%20iterator.next()%0D%0A%20%20const%20yield2%20%3D%20await%20iterator.next()%0D%0A%20%20const%20yield3%20%3D%20await%20iterator.next()%0D%0A%20%20const%20yield4%20%3D%20await%20iterator.next()%0D%0A%0D%0A%20%20%2F%2F%20IteratorResult%3C1%20%7C%202%20%7C%203%3E%0D%0A%20%20type%20ExpectedYield4Type%20%3D%20typeof%20yield4%0D%0A%20%20%2F%2F%20%201%20%7C%202%20%7C%203%0D%0A%20%20type%20ExpectedYield4ValueType%20%3D%20typeof%20yield4.value%0D%0A%0D%0A%20%20console.log('yield4'%2C%20yield4)%20%2F%2F%20%7B%20value%3A%20undefined%2C%20done%3A%20true%20%7D%0D%0A%20%20console.log('yiled4.value'%2C%20yield4.value)%20%2F%2F%20undefined%0D%0A%20%20if%20(yield4.value%20%3D%3D%3D%20undefined)%20%7B%0D%0A%20%20%20%20throw%20new%20Error('yield4.value%20type%20is%201%20%7C%202%20%7C%203%2C%20so%20value%20cannot%20be%20undefined')%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconsume()https://www.typescriptlang.org/play/#src=async%20function*%20generate()%20%7B%0D%0A%20%20yield%201%0D%0A%20%20yield%202%0D%0A%20%20yield%203%0D%0A%7D%0D%0A%0D%0A%2F%2F%20AsyncIterableIterator%3C1%20%7C%202%20%7C%203%3E%0D%0Atype%20Out%20%3D%20ReturnType%3Ctypeof%20generate%3E%0D%0A%0D%0Aasync%20function%20consume()%20%7B%0D%0A%20%20const%20iterator%20%3D%20generate()%0D%0A%20%20const%20yield1%20%3D%20await%20iterator.next()%0D%0A%20%20const%20yield2%20%3D%20await%20iterator.next()%0D%0A%20%20const%20yield3%20%3D%20await%20iterator.next()%0D%0A%20%20const%20yield4%20%3D%20await%20iterator.next()%0D%0A%0D%0A%20%20%2F%2F%20IteratorResult%3C1%20%7C%202%20%7C%203%3E%0D%0A%20%20type%20ExpectedYield4Type%20%3D%20typeof%20yield4%0D%0A%20%20%2F%2F%20%201%20%7C%202%20%7C%203%0D%0A%20%20type%20ExpectedYield4ValueType%20%3D%20typeof%20yield4.value%0D%0A%0D%0A%20%20console.log('yield4'%2C%20yield4)%20%2F%2F%20%7B%20value%3A%20undefined%2C%20done%3A%20true%20%7D%0D%0A%20%20console.log('yiled4.value'%2C%20yield4.value)%20%2F%2F%20undefined%0D%0A%20%20if%20(yield4.value%20%3D%3D%3D%20undefined)%20%7B%0D%0A%20%20%20%20throw%20new%20Error('yield4.value%20type%20is%201%20%7C%202%20%7C%203%2C%20so%20value%20cannot%20be%20undefined')%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconsume()
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Ron Buckton (@rbuckton)https://github.com/rbuckton
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Needs InvestigationThis issue needs a team member to investigate its status.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Investigation%22
Needs More InfoThe issue still hasn't been fully clarifiedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20More%20Info%22
on Feb 24, 2024https://github.com/microsoft/TypeScript/issues/29982#event-11909359598
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/29982
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/29982
Ron Buckton (rbuckton)https://github.com/rbuckton
Needs InvestigationThis issue needs a team member to investigate its status.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Investigation%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.